iPhone 游戏用什么来播放声音?
我有一款性能密集型 iPhone 游戏,我想为其添加声音。 似乎有大约三个主要选择:(1)AVAudioPlayer
,(2)音频队列和(3)OpenAL。 我不想仅仅为了播放示例而编写几页低级代码,因此我想使用 AVAudioPlayer
。 问题是,它似乎会影响性能 - 我使用 CFAbsoluteTimeGetCurrent 进行了简单的测量,并且 play
消息似乎需要 9 到 30 毫秒才能完成。 考虑到 25 毫秒 == 40 fps,这真是太痛苦了。
当然,有一个可以加快速度的 prepareToPlay
方法。 这就是为什么我编写了一个简单的类,它保留了几个 AVAudioPlayer
供其使用,预先准备好它们,然后使用准备好的播放器播放示例。 没有雪茄,仍然需要我上面提到的大约 20 毫秒。
这样的性能对于游戏来说是无法使用的,那么在iPhone上用什么来播放性能还不错的声音呢? 我对 AVAudioPlayer
做错了什么吗? 您使用音频队列播放声音吗? (在 2.2 发布之前,我已经写过一些类似于 AVAudioPlayer 的东西,我很乐意保留这种体验。) 你使用 OpenAL 吗? 如果是,是否有一种简单的方法来使用 OpenAL 播放声音,或者您是否必须编写几页代码?
更新:是的,使用 OpenAL 播放声音相当简单。
I have a performance-intensive iPhone game I would like to add sounds to. There seem to be about three main choices: (1) AVAudioPlayer
, (2) Audio Queues and (3) OpenAL. I’d hate to write pages of low-level code just to play a sample, so that I would like to use AVAudioPlayer
. The problem is that it seems to kill the performace – I’ve done a simple measuring using CFAbsoluteTimeGetCurrent
and the play
message seems to take somewhere from 9 to 30 ms to finish. That’s quite miserable, considering that 25 ms == 40 fps.
Of course there is the prepareToPlay
method that should speed things up. That’s why I wrote a simple class that keeps several AVAudioPlayer
s at its disposal, prepares them beforehand and then plays the sample using the prepared player. No cigar, still it takes the ~20 ms I mentioned above.
Such performance is unusable for games, so what do you use to play sounds with a decent performance on iPhone? Am I doing something wrong with the AVAudioPlayer
? Do you play sounds with Audio Queues? (I’ve written something akin to AVAudioPlayer before 2.2 came out and I would love to spare that experience.) Do you use OpenAL? If yes, is there a simple way to play sounds with OpenAL, or do you have to write pages of code?
Update: Yes, playing sounds with OpenAL is fairly simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
AVAudioPlayer 对于游戏音频来说非常边缘化。 通过调整其中一个示例来解决 AudioQueue 或 OpenAL 绝对是一种可行的方法。 这样延迟就更容易控制。
AVAudioPlayer is very marginal for game audio. Tackling AudioQueue or OpenAL by adapting one of the examples is definitely the way to go. latency is much more controllable that way.
如果您在主线程上调用
play
,请尝试在单独的线程上运行它。 我最终做的是:解决了我遇到的最严重的帧速率卡顿问题。
If you're calling
play
on the main thread, try running it on a separate thread. What I ended up doing is:which fixed the worst of the framerate stuttering I was experiencing.
我使用 OpenAL 和 CrashLanding 示例代码附带的类。 到目前为止,它可以同时播放样本和循环音乐,效果很好。 我目前正在学习如何释放分配给声音(.wav 文件)的内存,例如,当我想播放一些介绍音乐一次时。
I use OpenAL and the classes that came with the CrashLanding sample code. It's worked fine so far to play samples and play looped music all at the same time. I'm currently learning how to release the memory I've allocated for a sound (.wav file) when, for example, I want to play some intro music just once.
使用 CocosDenshion – 它免费、简单且有效。 它包装了用于背景音轨的
AVAudioPlayer
和用于声音的 OpenAL。Use CocosDenshion – it’s free, easy, and works. It wraps
AVAudioPlayer
for background tracks and OpenAL for sounds.您想检查您正在使用的实现的缓冲吗? 这可能与您遇到的 20 毫秒延迟有关。 即,尝试调整缓冲区大小。
Do you want to check the buffering with the implementation you're using? It might be somehow related to the 20ms delay you're experiencing. i.e., try to play around with the buffer size.