AVAudioPlayer导致游戏延迟 [英] AVAudioPlayer causes game lag

查看:136
本文介绍了AVAudioPlayer导致游戏延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的游戏,您在火箭里,可以避免屏幕上掉落的行星.当我播放短短的亚秒长声音时,我的游戏在播放时会出现抖动和滞后现象,然后恢复正常.有谁知道为什么会这样吗?

I have a simple game where you are in a rocket and you avoid planets that come down the screen. When I play a short sub second long sound my game jitters and lags whilst it plays it and then returns to normal. Does any one know why this might be?

推荐答案

AVAudioPlayer虽然声音在其他线程上播放,但会导致长时间的延迟,无法在其当前线程上初始化自身.

AVAudioPlayer, while the sound plays on a different thread, will cause a long delay initializing itself on its current thread.

我的解决方案是仅将AVAudioPlayer用于音乐,因为高级API对此很有用,但即使如此,还是要这样做:

My solution was to use AVAudioPlayer only for music, since the high level API made it useful for that, but even then to do a:

dispatch_async

当我想对播放音乐进行任何操作时,

进入特殊的音乐队列.

to a special music queue when I wanted to do any operations around playing music.

对于声音效果,此技术可以起作用(就不会引起抖动而言),但是声音会延迟并且可能不够好.

For sound effects this technique will work (in terms of not causing jitter), but the sound will be delayed and probably not be good enough.

我通过直接将OpenAL用于声音效果解决了这一问题.没有抖动,没有延迟.

I solved this by using OpenAL directly for sound effects. No Jitter, no lag.

这篇关于AVAudioPlayer导致游戏延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆