SoundPool Streams

时间:2012-11-22 18:35:32

标签: java android soundpool

所以我有soundpool OnLoadCompleteListener,其代码为mStreamId = soundPool.play(sampleId,1,1,1,0,1f);我有2个按钮,将播放不同的soundpools.I使用mStreamId停止soundpool。我的问题是,当我点击第一个按钮,它播放声音,我点击第二个按钮,它将停止第一个按钮的声音。这是我的代码:

    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);
    }

1 个答案:

答案 0 :(得分:0)

因为你说过了。 如果希望声音播放完成,请删除停止方法调用。 播放声音时,Soundpool将从池中返回下一个可用的流。