如何使用 SoundPool 同步声音

发布于 2024-10-05 23:19:01 字数 561 浏览 0 评论 0原文

我一直在尝试同时播放几种声音;目前我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凯凯我们等你回来 2024-10-12 23:19:01

我一次为一种声音做了声音池,它可能对你有帮助。
Android:声音池和服务

谢谢

i done the sound pool for one sound at time it might help you.
Android:sound pool and service

Thank you

最初的梦 2024-10-12 23:19:01

您需要了解 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))

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 use 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)
            }
        }
    }
}

To use:

loadAndPlay(soundPool, context, intArrayOf(R.raw.sound_1, R.raw.sound_2, R.raw.sound_3))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文