sounds won't play

sterlingy

Active Member
Licensed User
Longtime User
And I thought I'd make it a day without posting to this forum. No such luck.

My app plays sounds, and since I based it on the Asteroids app, I'm sure the code is correct as I haven't changed any of the audio code.

However, when I change the mp3 file to anything other than what the original Asteroids app used, nothing plays and there are no error messages.

What could the difference between the files be? They are roughly the same bitrate.

Also, I noticed in the documentation that SoundPool is used for short sounds. What if I want to have an entire song in the background. Is there a different approach to longer audio files?

-Sterling
 

basil99

Active Member
Licensed User
Longtime User
Hard to say, did you tried your mp3 with asteroid sample ?
I prefer to use OGG instead of mp3
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
B4X:
'Class module
Sub Class_Globals
   Public gv As GameView
   Public pad As GamePad
   Private mainTimer As Timer
   Private fps As Float
   fps = 30 'this value is not really important. It is just an approximate used when starting t
   Private ldoggieTime As Long
   Private lblFPS As Label
   Public lblAngle As Label
   Public Dogs As List
   Private DogsBitmaps(,) As BitmapData
   Public leroyBitmaps(,) As BitmapData
   Private CarsBitmaps(,) As BitmapData
   Public Cars As List
   Public MyLeroy As Leroy
   
   Public streetBackground As BitmapData
   Private sounds As SoundPool
--->   Public shotSoundId, breakSoundId, crashSoundId As Int
--->   Public backgroundMusic, sirenSFX, whistleSFX As Int
   Public backgroundScale As Int : backgroundScale = 1
   
End Sub

Public Sub Initialize (vGV As GameView, vPad As GamePad, vlblFPS As Label, vlblAngle As Label)
   GameUtils.init
   gv = vGV
   pad = vPad
   lblFPS = vlblFPS
   lblAngle = vlblAngle
   mainTimer.Initialize("MainTimer", 15)
   
   streetBackground = CreateBackground
   gv.BitmapsData.Add(streetBackground)
   
   DogsBitmaps = GameUtils.LoadSpritesFromSheet(LoadBitmap(File.DirAssets, "dog_spritesheet.png"), 6, 2)
   Dogs.Initialize
   
   CarsBitmaps = GameUtils.LoadSpritesFromSheet(LoadBitmap(File.DirAssets, "car_spritesheet.png"), 6, 1)
   Cars.Initialize
   
   Dim bd As BitmapData
   
   leroyBitmaps = GameUtils.LoadSpritesFromSheet(LoadBitmap(File.DirAssets, "leroy_spritesheet.png"), 6, 1)
   AddLeroy (50%x, 50%y, _
         50 * GameUtils.scale)
         

   'sounds are loaded to a SoundPool
   sounds.Initialize(4)
   shotSoundId = sounds.Load(File.DirAssets, "shotgun.mp3")
   breakSoundId = sounds.Load(File.DirAssets, "break.mp3")
--->   crashSoundId = sounds.Load(File.DirAssets, "crash.mp3")
   
   backgroundMusic = sounds.Load(File.DirAssets, "music.mp3")
--->   sirenSFX = sounds.Load(File.DirAssets, "siren.mp3")
   whistleSFX = sounds.Load(File.DirAssets, "whistle.mp3")
   
   
   'add the game pad 
   pad.Initialize(gv)
   Dim length, rightPadding, leftPadding, bottomPadding As Int
   length = 100 * GameUtils.scale 'also the panel's height
   rightPadding = 50 * GameUtils.scale
   leftPadding = 20 * GameUtils.scale
   bottomPadding = 20 * GameUtils.scale
   pad.AddToGameView (gv, 50%x - (length/2), 100%y - bottomPadding - length, _
      100%x - rightPadding - leftPadding, length)
   Main.padX = 50%x
   Main.padY = 100%y - bottomPadding - length + (length/2)
   
   StartGame
End Sub

Private Sub StartGame
   'Clear the previous remaining dogs.
   For i = 0 To Dogs.Size - 1
      Dim doggie As Dog
      doggie = Dogs.Get(i)
      doggie.RemoveFromGameView 'This method will remove the Dog from the GameView
   Next
   Dogs.Clear
   'Create 4 new Dogs
   For i = 1 To 4
      AddDog (Rnd(1%x, 99%x), Rnd(1%y, 99%y), _
         30 * GameUtils.scale)
   Next
   
   'Clear the previous remaining cars.
   For i = 0 To Cars.Size - 1
      Dim vehicle As Car
      vehicle = Cars.Get(i)
      vehicle.RemoveFromGameView 'This method will remove the Dog from the GameView
   Next
   Cars.Clear
   'Create 4 new Cars
   Dim dir As Int 'set direction of car
   dir = 0
   For i = 1 To 4
      If i > 2 Then 
         dir = 1 
      End If
      AddCar (5%x + (i * 15%x), Rnd(1%y, 99%y), _
         40 * GameUtils.scale, dir)
   Next
   
   'start music
--->   PlaySound(sirenSFX, 1)

End Sub

.
.
.
.
.


Public Sub StartTimer
   mainTimer.Enabled = True
End Sub

Public Sub StopTimer
   mainTimer.Enabled = False
End Sub

Private Sub mainTimer_Tick
   'calculate the frames per second
   fps = (1000 / Max(10, (DateTime.Now - ldoggieTime)) + 20 * fps) / 21
   lblFPS.Text = NumberFormat(fps, 0, 0)
   ldoggieTime = DateTime.Now
   Dim doggie As Dog
   Dim i As Int

.
.
.
.
.

   'mark the GameView as "dirty". This will cause it to redraw itself.
   gv.Invalidate
   
End Sub

Public Sub PlaySound(sound As Int, volume As Float)
   sounds.Play(sound, volume, volume, 0, 0, 1)
End Sub

'Load street into background
Private Sub CreateBackground As BitmapData
   'The background is created manually with a bitmap and canvas.
   Dim bd As BitmapData
   
   bd.Bitmap.InitializeMutable(100%x / backgroundScale, 100%y / backgroundScale) 'create a wide image
   bd.Bitmap = LoadBitmap(File.DirAssets, "streetmap.png")
   bd.srcRect.Initialize(0, 0, bd.Bitmap.Width, 62%y)
   bd.destRect.Initialize(0, 8%y, 100%x, 70%y)
   Return bd
End Sub

You can see in the above code that I added some arrows pointing to the relevant lines.

crash.mp3 is from the original Asteroids app. At the end of sub StartGame, I initiate the sirenSFX sound, but it won't play. If I change this to crashSoundID, it works.

And now for the crazy bit, if I change the file name from siren.mp3 to crash.mp3, then it works just fine.

On the other hand, if I call the PlaySound sub, from another class, then the sirenSFX sound will play.

-S
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
I stripped down my code, and now it's stranger. It will play the audio, but only when you exit the program.

See attached

-Sterling
 

Attachments

  • AudioProblem.zip
    239.4 KB · Views: 217
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Erel,

I did check the unfiltered log, and there's nothing unusual there. The files are about the same size as the Asteroid sound files. They do play, just not when I want them to. See my last post with attachment.

Also, if some files can be too large, I assume a three minute song fits into that category. How do you play a long piece of music?

-Sterling
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
You should use MediaPlayer for music files.

Thanks. I'll look into it.

Let me know if you have a clue as to why my SFX won't play until I exit the app.

-Sterling
 
Upvote 0
Top