Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop playing a Sound via Soundpool?

Please, have a look at those pieces of code:

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 press the button:

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

if I press this button twice in the same time, it plays the sound togheter, I want to stop the sound and play again if the user press the button while playing

I tryed puting an

soundPool.stop(soundID); 

before the soundPool.play but I don't know why it only work for the 1 time the song is played, do you guys have any ideia why this works only for the 1 time? and how I can solve this? thanks

like image 440
CésarQ Avatar asked Jul 27 '12 19:07

CésarQ


1 Answers

The method stop() takes the stream ID of a currently playing stream, not the sound ID you have loaded. 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);

Would stop the currently playing track and start another. A second option would be to limit the number of streams your SoundPool allows. If you instantiate your SoundPool with a maxStreams value of 1, then any currently playing sound will stop when you attempt to start another, which may implement your desired behavior more cleanly.

like image 199
devunwired Avatar answered Nov 09 '22 14:11

devunwired