Android SoundPool 有多糟糕?使用什么替代方案?
我正在将 Android 的 SoundPool
作为在我的通用游戏开发库中实现声音效果的机制。这似乎很理想。
但一点点研究表明所有 种类 SoundPool 中的错误。 SoundPool
中的错误仍然相关吗?
因为我正在开发一个库,所以 SoundPool
中的任何错误都会成为我的库中的错误,并且我希望让我的用户免受这些错误的影响。
所以我的问题基本上是:我应该使用什么 API 来处理音频?
使用 AudioTrack
并编写自己的混音器并不是不可能的。但显然最好避免这样做。有没有API可以为我提供解码?
我需要能够同时播放合理数量的音效(至少 16 个,比如说),并且有更多的开放性。声音需要以低延迟开始播放。需要支持 WAV
文件(MP3/Ogg 不重要)。音效需要支持无缝循环和动态、单独的音量调整。 Android 应用程序生命周期需要得到适当的支持。
我听说 SoundPool
的某个地方有 1MB 的限制,这对于每个单独的音效来说可能是可以接受的,但不适用于所有缓冲区/声音。谁能告诉我具体的限制是多少?
最后,我还需要能够在低 CPU 负载的情况下以压缩格式播放背景音乐。我认为 MediaPlayer 非常适合此目的。它可以与其他 API 并行使用吗?
我知道有些人一直在使用 MediaPlayer
来填充 SoundPool
。但它支持我需要的功能吗?
还有其他我错过的音频 API 吗?
I was looking at Android's SoundPool
as a mechanism to implement sound effects in my generic game development library. It seemed ideal.
But a little bit of research indicates that there all kinds of bugs in SoundPool
. Are the bugs in SoundPool
still relevant?
Because I'm developing a library, any bugs in SoundPool
become bugs in my library, and I want to insulate my users from that.
So my question is basically: what API should I use for audio?
Using AudioTrack
and writing my own mixer is not out of the question. But obviously it would be preferable to avoid doing that. And is there any API to provide decoding for me?
I need to be able to play a reasonable number of simultaneous sound effects (at least 16, let's say), and have even more open. Sounds need to start playing with low latency. WAV
files need to be supported (MP3/Ogg is unimportant). Sound effects need to support seamless looping and dynamic, individual volume adjustment. The Android app lifecycle needs to be properly supported.
I have heard there is a 1MB limit somewhere for SoundPool
, this is probably acceptable for each individual sound effect but not for all buffers/sounds. Can someone tell me exactly what the limit is on?
Finally, I need to be able to play background music as well, in compressed formats, with low CPU load. I assume MediaPlayer
is ideal for this. Can it be used in parallel with another API?
I know a few people have been using MediaPlayer
to fill in for SoundPool
. But does it support the features that I need?
Are there any other audio APIs I've missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是添加一些有关此问题的最新反馈。我已经在一个应用程序中使用 SoundPool 一段时间了,该应用程序具有相当大的按键声音用户群。我们的用例:
setRate
[0.5f-2.0f]
我现在已经经历了两个主要的设备特定问题,并决定减少损失并放弃 SoundPool
setRate
与rate > 一起使用, 1.0f
SoundPool 开始抛出异常,直到您的应用程序退出(并在此过程中消耗大量电池)。TL;博士;我不再认为值得冒着调试 SoundPool 的危险/麻烦
Just to add some more recent feedback on this issue. I've been using SoundPool for some time in an app with a fairly large user base for key press sounds. Our use case:
setRate
across it's full range[0.5f-2.0f]
I've now experienced two major device specific issue and have decided to cut my losses and switch away from SoundPool
setRate
withrate > 1.0f
the SoundPool with start throwing exceptions until your app quits (and burn through a bunch of battery in the process).TL;DR; I no longer think it's worth the danger/hassle of debugging SoundPool
坚持使用 OGG 文件,SoundPool 就可以满足您的要求。 Android 是多平台野兽的本质,无论程序员多么努力地尝试,其硬件配置都无法适用于每个重要的程序。
如果这是一个大型且资金充足的项目,请为每部主要手机添加用于测试的资金。实际上,这比程序员花在研究和尝试猜测其性能上的时间要便宜得多。
对不起。似乎这不是您正在寻找的答案。祝你好运!
Stick with OGG files and SoundPool will do you just fine. It's the nature of the multi-platform beast that is Android that there WILL be hardware configurations that will not work with every significant program, no matter how diligently the programmers try.
If this is a large and well-funded project, add to the funding one of each major phone for testing. It's actually much cheaper than the programmer time spent researching and trying to guess what their performance is.
Sorry. Seems as if this isn't the answer that you were looking for. Good luck!
免责声明:我对 MediaPlayer 有少量经验,对我提到的其他 API 没有成功的经验,以下信息基于我在 DOC 中阅读的内容以及我从 google 搜索中阅读的内容。
您可以将媒体播放器(用于背景音乐)与其他音频 API 一起使用,因为 MediaPlayer 自动在它自己的线程上运行,但我相信它的 cpu 负载很高,而且我认为它不会很好地占用压缩位,但我不太确定。
还有 JetPlayer http://developer.android.com/reference/android/media/ JetPlayer.html 看起来要有效使用需要做很多工作,但它可以很好地播放背景音乐,然后根据游戏需要播放其他声音。从我读到的 DOC 来看,它需要一个 MIDI 文件(我认为?),然后您将轨道静音和取消静音,以使其按照您想要的方式工作。
我喜欢 AudioTrack,因为它使您能够通过更改声音的频率来在运行时编辑声音,而 SoundPool 也可以做到这一点。
尽管对于您的情况,AudioTrack 似乎效果不佳,因为播放两个声音需要两个线程,因为 AudioTrack 正在阻塞(我很确定)。
对于 SoundPool,我认为既然您有 16 个声音,也许可以采用两个线程,每个线程中有一个 SoundPool,并向每个 SoundPool 应用 8 个声音。但我真的不知道,因为我从未尝试过使用 SoundPool。
再说一次,我的信息不是基于经验,而是基于我所读到的内容,所以我可能完全错误,也可能只是稍微错误,或者哎呀,谁知道呢。
我对 SoundPool 的错误一无所知,因为我还没有研究过它。
DISCLAIMER: I have a small amount of experience with MediaPlayer, and no successful experience with the other APIs I mention, and the following information is based on what I've read in the DOCs and what I've read from google searches.
You could use mediaplayer (for the background music) with other audio APIs, since MediaPlayer automatically runs on it's own thread, but I believe it has a high-ish cpu load, and I don't think it would take compressed bits very well, but I'm not too sure.
There's also JetPlayer http://developer.android.com/reference/android/media/JetPlayer.html which seems like a lot of work to use effectively, but it would work very well with playing background music, then playing other sounds as needed in the game. From what I read of the DOCs, it takes a MIDI file (I think?) and you mute and unmute tracks to make it work how you want it to.
I like AudioTrack because it gives you the ability to edit sounds at runtime by changing the frequencies of the sound, and SoundPool can do the same.
Though for your situation, AudioTrack doesn't seem like it would work well, since playing two sounds would require two threads because AudioTrack is blocking (I'm pretty sure).
And with SoundPool, I'm thinking that since you have 16 sounds, maybe take two threads with one SoundPool in each thread and apply 8 sounds to each SoundPool. I don't really know though, as I've never even tried using SoundPool.
And again, my information is not based on experience, just what it appears from what I've read, so I may be completely or maybe just slightly wrong, or heck, who knows.
And I don't really know anything about the SoundPool bugs, since I haven't researched it.