SpriteKit游戏中的AVAudioPlayer和性能问题 [英] AVAudioPlayer and performance issue in SpriteKit game

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

问题描述

我的AVAudioPlayer有问题,在我的spritekit游戏中播放短音。我有非常动态的游戏场景,当用户点击特定元素时,我想播放简单的嘟嘟声,但我注意到使用AVAudioPlayer执行声音会导致严重的性能问题。

I have problem with AVAudioPlayer and playing the short sounds in my spritekit game. I have quite dynamic game scene and when the user tap into particular element I want to play simple "beep" sound, but I notice that performing sound using AVAudioPlayer cause significant performance issue.

我的音频文件是.mp3格式。

My audio files are in .mp3 format.

我也尝试过SKAction方法(playSoundFileNamed)来执行声音和一切看起来几乎一样,我注意到了同样的性能问题。

Also I have tried SKAction method (playSoundFileNamed) to perform sound and everything looks almost the same, I noticed the same performance issue.

我的问题是在SpriteKit中执行声音的最佳做法是什么。

My question is what is the best practice to perform sound in SpriteKit.

PS:我试图找到WWDC Adventure spritekit示例代码来检查它们如何解决播放声音的问题,但遗憾的是示例代码不再可用。

PS: I tried to find WWDC Adventure spritekit sample code to check how they resolve problem with playing sounds, but unfortunately sample code is no longer available.

推荐答案

您可能会因为没有预加载声音文件而导致滞后,因此在创建/播放时会出现延迟/延迟。最佳做法是通常在应用程序启动时预加载音频文件,以便它们可以立即播放。

You are probably getting lag because you are not preloading your sound files and therefore get some lag/delay when creating/playing them. The best practice is to usually preload your audio files at app launch so that they are ready to play immediately.

因此对于AVPlayers而言,只需将它们设置为应用程序启动而不是就在播放之前。当您想要播放音乐时,您只需播放AVPlayer。

So for AVPlayers just set them all up at app launch rather than just before playing them. Than when you want to play the music you just play the AVPlayer.

myAVPlayer1.play()

关于SKAction.play ...同样的问题。您需要创建对动作的引用而不是直接调用它

In regards to SKAction.play... its the same issue. You will need to create a reference to your action rather than calling it directly

所以在您的gameScene上面的DidMoveToView中创建声音属性

So in your gameScene above DidMoveToView you create your sound properties

class GameScene: SKScene {

     let sound1 = SKAction.playSoundFileNamed("Test", waitForCompletion: false)

     ....
}

并且比你游戏中正确的位置运行它

and than in your game at the correct spot you run it

runAction(sound1)

这种方式应该没有延迟,因为声音已经预先加载。

This way there should be no lag because the sound is already preloaded.

希望这会有所帮助

这篇关于SpriteKit游戏中的AVAudioPlayer和性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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