我一直在尝试同时播放几个声音;目前我正在使用一个共享的SoundPool实例。我希望1,2或3的声音在完全相同的时间播放,没有任何滞后。
调用SoundPool.play(.)连续的X次,声音按顺序播放,就像你想的那样。什么是正确的实现这一点,我可以准备所有的声音,在同一时间播放,然后把它们作为一个?
Sudo代码:
SoundPool _soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
_soundPool.load(_context, soundId1, 1);
_soundPool.load(_context, soundId2, 1);
_soundPool.load(_context, soundId3, 1);
_soundPool.play(soundId1, vol, vol, 1, 0, 1f);
_soundPool.play(soundId2, vol, vol, 1, 0, 1f);
_soundPool.play(soundId3, vol, vol, 1, 0, 1f);发布于 2011-06-13 04:37:43
我做了一个声音池,它可能会对你有帮助。Android:sound pool and service
谢谢
发布于 2020-04-06 09:42:56
您需要理解SoundPool.load方法是异步的,因此,当您连续调用它3次,然后调用play时,就无法保证这听起来确实加载了。所以你需要等到所有的声音都被加载。为此,请使用OnLoadCompleteListener
fun loadAndPlay(soundPool: SoundPool, context: Context, resIds: IntArray) {
val soundIds = IntArray(resIds.size) {
soundPool.load(context, resIds[it], 1)
}
var numLoaded: Int = 0
soundPool.setOnLoadCompleteListener { sPool, sampleId, status ->
numLoaded++
if (numLoaded == resIds.size) {
soundPool.setOnLoadCompleteListener(null)
for (id in soundIds) {
soundPool.play(id, 1f, 1f, 1, 0, 1f)
}
}
}
}使用:
loadAndPlay(soundPool, context, intArrayOf(R.raw.sound_1, R.raw.sound_2, R.raw.sound_3))https://stackoverflow.com/questions/4342491
复制相似问题