1

我有 soundpool 将从 sd 卡加载文件,我使用 mstreamID 来停止它,我在 OnLoadCompleteListener 中使用 mstreamID 以便可以加载文件。我使用 2 个按钮来播放 soundpool 不同的声音。但是,当我单击第一个按钮并播放 1 个声音时,我单击第二个按钮,它会停止第一个按钮的声音。

这是我的代码:

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);
        }
// et1 is extended from Settings.java
        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);
    }
4

1 回答 1

1

请阅读文档,您想说什么?

  1. 如果您不卸载同一个文件,请不要多次加载它。
  2. 使用 SoundPool 时在开始时加载所有声音。
  3. streamId 一直在增加,你永远不会得到相同的 streamId。
于 2012-11-23T15:33:19.880 回答