如何使用 SoundPool 同步声音
我一直在尝试同时播放几种声音;目前我正在使用 SoundPool 的共享实例。我希望同时播放 1、2 或 3 个声音,没有延迟。
当连续调用 SoundPool.play(...) X 次时,声音会按照人们想象的顺序播放。实现这一目标的正确方法是什么,我可以准备同时播放的所有声音,然后将它们作为一个播放?
须藤代码:
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);
I have been trying to get a few sounds to play at the same time; currently i'm using a shared instance of SoundPool. I would like 1, 2 or 3 sounds to be played at the exact same time with no lag.
When calling SoundPool.play(...) X number of times in succession, the sounds are played in that order as one might think. what is the proper what to achieve this where i can prepare all of the sounds to be played at the same time and then play them as one?
Sudo Code:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一次为一种声音做了声音池,它可能对你有帮助。
Android:声音池和服务
谢谢
i done the sound pool for one sound at time it might help you.
Android:sound pool and service
Thank you
您需要了解
SoundPool.load
方法是异步的,因此当您连续调用该方法 3 次,然后调用 play 时,无法保证该声音确实已加载。所以你需要等待所有声音加载完毕。为此,请使用OnLoadCompleteListener
:使用:
You need to understand that
SoundPool.load
method is asyncronous, so when you call it 3 times in a row, and then call play, there is no garantee that this sounds actually loaded. So you need to wait until all sounds is loaded. For that useOnLoadCompleteListener
:To use: