2012-07-27 988 views
4

請看看這些代碼片段:如何通過Soundpool停止播放聲音?

private SoundPool soundPool; 
private int soundID; 

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

,當我按下按鈕:

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

,如果我按兩次在同一時間這個按鈕,它播放的聲音togheter ,我想停下來的聲音並再次播放,如果用戶按下按鈕,同時播放

我tryed puting的

soundPool.stop(soundID); 

之前soundPool.play,但我不知道爲什麼它只適用於歌曲播放的1次,你們有任何理想爲什麼這隻適用於1次?以及我如何解決這個問題?謝謝

回答

15

方法stop()獲取當前播放流的流ID,而不是您已加載的聲音ID。所以像這樣:

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

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

會停止當前播放的曲目,並開始另一個。第二種選擇是限制您的SoundPool允許的數據流。如果您將SoundPool實例化爲maxStreams的值爲1,則當您嘗試啓動另一個時,任何當前播放的聲音都會停止,這可能會更乾淨地實現您所需的行爲。

+0

謝謝,它的工作,對不起我的noobiness ^^ – 2012-07-27 19:14:42

+0

謝謝。這解決了我的遊戲中的小錯誤。 – 2014-10-19 04:54:08

0

你能看到代碼

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