How to stop playing sound through Soundpool?

Please look at these code snippets:

private SoundPool soundPool; private int soundID; soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundID = soundPool.load(this, R.raw.batimanbailedosenxutos, 1); 

and when I click the button:

 soundPool.play(soundID, volume, volume, 1, 0, 1f); 

if I press this button twice at the same time, it plays the sound to switch, I want to stop the sound and play again if the user presses the button during playback

I tried to put

 soundPool.stop(soundID); 

in front of soundPool.play, but I don’t know why it only works for 1 time, when the song is playing, do you guys have any idea why this works only 1 time? and how can i solve this? thanks

+4
source share
2 answers

The stop() method accepts the stream identifier of the currently playing stream, not the identifier of the sound you downloaded. So something like this:

 int streamId = soundPool.play(soundID, volume, volume, 1, 0, 1f); soudPool.stop(streamId); streamId = soundPool.play(soundID, volume, volume, 1, 0, 1f); 

Stop playing the current track and start another. The second option is to limit the number of streams available by your SoundPool . If you create a SoundPool instance with maxStreams to 1, then any sound currently playing will be stopped when you try to start another, which can more clearly implement the desired behavior.

+15
source

You can see the code

 public static void clear() { if (mSoundManager != null) { mSoundManager.mSoundPool = null; mSoundManager.mAudioManager = null; mSoundManager.mSoundPoolMap = null; } mSoundManager = null; } 
0
source

All Articles