6

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

CésarQ
  • 107
  • 1
  • 1
  • 6

3 Answers3

19

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.

devunwired
  • 62,780
  • 12
  • 127
  • 139
0

You Can See Code

public static void clear() {
        if (mSoundManager != null) {
            mSoundManager.mSoundPool = null;
            mSoundManager.mAudioManager = null;
            mSoundManager.mSoundPoolMap = null;
        }
        mSoundManager = null;
    }
laalto
  • 150,114
  • 66
  • 286
  • 303
Quang Long
  • 31
  • 3
0

I did it by loading the soundpool track id again..

mSoundPool.Stop(trackID);
trackID.load(getApplicationContext(),R.raw.audioclip,1;

And you can again play the mSoundPool.play() anyWhere.

Sayed Muhammad Idrees
  • 1,245
  • 15
  • 25