android - Soundpool 不同的 streamid

标签 android 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);
    }

最佳答案

请阅读文档,你想说的是什么?

  1. 如果不卸载同一文件,则不要多次加载它。
  2. 使用 SoundPool 时在启动时加载所有声音。
  3. streamId 一直在增加,你永远不会得到相同的 streamId。

关于android - Soundpool 不同的 streamid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13518938/

相关文章:

android - ICS 日历契约(Contract) RDATE 格式?

android - Activity状态丢失,部分设备Illegal state异常

安卓 : How to programmatically set layout_constraintRight_toRightOf "parent"

java - 无法使用 STREAM_VOICE_CALL 通过 Android Java 中的 MediaPlayer 播放音频

java - Firestore - 在本地合并两个查询

android - 如何判断哪个Activity启动了某个服务?

Android L SoundPool.load() 回归

Android Soundpool - 循环播放异常

android - Intent 加载时声音未响起

java - 有时,与ScheduledExecutorService一起使用的Android SoundPool会停止播放声音