Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop all sounds in SoundPool?

I am using SoundPool to play sfx sounds in a game on Android. In most cases it works perfectly, except sometimes I need to stop all sounds at once not pause (doesn't matter if they are set to loop or not). I can't figure out how to stop a sound from playing without knowing the StreamID of that sound. What I know:

  • soundpool.load(...some sound...) returns a soundID
  • soundpool.play(soundID) plays the sound and returns a streamID
  • soundpool.stop(streamID) stops the sound

My question is, how can I stop a sound without knowing the streamID ? I tried tracking all streamIDs in a list, but sometimes there are so many short streams playing at once, that it won't work. And I can't find any method in SoundPoolto get the active streamIDs. Does anyone know how to stop all sounds? Any hint is appreciated! thanks

like image 805
GameDroids Avatar asked Sep 13 '13 19:09

GameDroids


2 Answers

I'd recommend using autoPause() and autoResume() to pause and restart your sounds. This function is part of the soundpool:

http://developer.android.com/reference/android/media/SoundPool.html#autoPause()

like image 125
Shygar Avatar answered Nov 15 '22 00:11

Shygar


What about using .release() ? Accourding to the documentation:

Release the SoundPool resources. Release all memory and native resources used by the SoundPool object. The SoundPool can no longer be used and the reference should be set to null.

I think it also stops everything. (Actually it was causing a bug at one of my apps, that's why I say it)

However, I believe that if you want to play any sounds later, you may have to load them again.

like image 33
anestv Avatar answered Nov 15 '22 00:11

anestv