How to stop all sounds in SoundPool?

I use SoundPool to play sfx sounds in an Android game. In most cases, it works fine, except that sometimes I need to stop all sounds at the same time without a pause (it does not matter if they are set in a loop or not). I can’t figure out how to stop a sound from playing without knowing the StreamID that sound. What i know:

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

My question is: how can I stop the sound without knowing the streamID ? I tried to keep track of all the data streams in the list, but sometimes there are so many short streams that they will not work. And I can not find any method in SoundPool to get active streamID. Does anyone know how to stop all sounds? Any hint is appreciated! thanks

+6
source share
2 answers

How about using .release() ? Satisfaction of documentation:

Release SoundPool resources. Release all memory and native resources used by the SoundPool. SoundPool can no longer be used, and the link should be set to null.

I think this also stops everything. (This actually caused an error in one of my applications, so I say this)

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

+3
source

I would recommend using the autoPause () and autoResume () functions to pause and restart the sound. This feature is part of soundpool:

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

+3
source

Source: https://habr.com/ru/post/953800/


All Articles