gpt4 book ai didi

java - Android:在 SoundPool 中无限播放声音停止后第二次不播放

转载 作者:太空狗 更新时间:2023-10-29 15:20:08 28 4
gpt4 key购买 nike

我正在尝试通过 SoundPool 播放 2 种声音。

以下测试代码使第二个播放没有声音。只有当我在 HTC Hero 设备和模拟器上无限播放声音时才会发生这种情况。我正在使用 android 1.6。

...
SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
int soundId1 = soundPool.load(getApplicationContext(), R.raw.sound1, 1);
int soundId2 = soundPool.load(getApplicationContext(), R.raw.sound2, 1);

// the first one plays
int streamId = soundPool.play(soundId1, 1.0f, 1.0f, 1, -1, 1.0f);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
soundPool.stop(streamId);

// the second one doesn't play
streamId = soundPool.play(soundId2, 1.0f, 1.0f, 1, -1, 1.0f);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
soundPool.stop(streamId);
...

最佳答案

查看代码中的这一行以获取第一个声音...

int streamId = soundPool.play(sound1, 1.0f, 1.0f, 1, -1, 1.0f);

According to this link ,第5个参数定义循环模式。 0 表示无循环或 -1 表示永远循环。您的代码显示 -1,因此第一个声音会永远循环播放,因此第二个声音不会播放。尝试将第一个声音的循环模式更改为无循环,即。 0.

编辑:我想我知道你的问题。当您尝试播放声音时,样本尚未准备好,因此,您需要实现一个 onLoadCompleteListener,以便样本在准备就绪时播放。示例。

SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int arg2) {
streamId = soundPool.play(sampleId, 1.0f, 1.0f, 1, -1, 1.0f);
}

});
int soundId1 = soundPool.load(getApplicationContext(), R.raw.tick, 1);
int soundId2 = soundPool.load(getApplicationContext(), R.raw.tock, 1);

现在加载这些声音后,将播放它们。我已经对此进行了测试,并且两种声音都会播放,因为听众会确保它们在播放之前已加载。

将此代码集成到您的代码中,应该可以解决问题。如果没有,请告诉我,我会尝试找到其他解决方案:)

关于java - Android:在 SoundPool 中无限播放声音停止后第二次不播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8680230/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com