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

查看:22
本文介绍了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.

因此,对于 AVPlayer,只需在应用启动时进行设置,而不是在播放之前进行设置.当你想播放音乐时,你只需播放 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

因此,在 DidMoveToView 上方的 gameScene 中,您可以创建声音属性

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天全站免登陆