问题
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
回答1:
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.
回答2:
You Can See Code
public static void clear() {
if (mSoundManager != null) {
mSoundManager.mSoundPool = null;
mSoundManager.mAudioManager = null;
mSoundManager.mSoundPoolMap = null;
}
mSoundManager = null;
}
回答3:
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.
来源:https://stackoverflow.com/questions/11694110/how-to-stop-playing-a-sound-via-soundpool