2012-11-22 87 views
0

所以我有soundpool OnLoadCompleteListener它有這個代碼mStreamId = soundPool.play(sampleId,1,1,0,1f);我有2個按鈕,將發揮diffrent soundpools.I使用mStreamId停止soundpool.My問題是當我點擊第一個按鈕,它播放聲音,我點擊第二個按鈕,它會停止第一個按鈕的聲音。這裏是我的代碼:SoundPool Streams

public class MainActivity extends Settings { 
     SoundPool mSoundPool; 
     int mSoundId; 
     int mStreamId = 0; 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); 
      mSoundPool 
        .setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 
         public void onLoadComplete(SoundPool soundPool, 
           int sampleId, int status) { 
          mStreamId = soundPool.play(sampleId, 1, 1, 1, 0, 1f); 
} 
       }); 

    } 
//OnClick 
public void button1(View view) { 
     if (mStreamId != 0) { 
      mSoundPool.stop(mStreamId); 
     } 

     String path = getFullFilePath(getApplicationContext(), et1.getText() 
       .toString()); 
     mSoundId = mSoundPool.load(path, 1); 
    } 
public void button2(View view) { 
     if (mStreamId != 0) { 
      mSoundPool.stop(mStreamId); 
     } 

     String path = getFullFilePath(getApplicationContext(), et2.getText() 
       .toString()); 
     mSoundId = mSoundPool.load(path, 1); 
    } 

回答

0

因爲你告訴它。 如果要讓聲音播放完成,請移除停止方法呼叫。 Soundpool會在播放聲音時從池中返回下一個可用流。