How to stop playing a Sound via Soundpool?

此生再无相见时 提交于 2019-12-19 19:47:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!