AVAudioPlayer and performance issue in SpriteKit game

I have a problem with AVAudioPlayer and playing short sounds in my spritekit game. I have a fairly dynamic game scene, and when a user clicks on a specific element, I want to play a simple sound, but I notice that playing sound using AVAudioPlayer causes a significant performance problem.

My audio files are in .mp3 format.

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

My question is what is the best way to use sound in SpriteKit.

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

0
source share
3 answers

You are probably getting lag because you are not pre-loading your sound files and therefore get a delay / delay when creating / playing. The best practice is to preload your audio files at application startup so that they are ready for immediate playback.

So, for AVPlayers, just set them to run applications, not just before playing them. Then, when you want to play music, you simply play AVPlayer.

myAVPlayer1.play() 

As for SKAction.play ... its the same problem. You will need to create a link to your action, rather than calling it directly.

So in your game Scene above DidMoveToView you create your sound properties

 class GameScene: SKScene { let sound1 = SKAction.playSoundFileNamed("Test", waitForCompletion: false) .... } 

and than in your game in the right place, you run it

 runAction(sound1) 

Thus, there should be no delay because the sound is already preloaded.

Hope this helps

+1
source

I think you just need to see which triggers this action . If your sprite is involved in other actions or some context animations, you can try to start your sound with SKNode , which places your sprite. The best way is to always use reasonable mp3 sizes.

PS: I usually prefer to use this extension to control also the volume:

 public extension SKAction { public class func playSoundFileNamed(fileName: String, atVolume: Float, waitForCompletion: Bool) -> SKAction { let nameOnly = (fileName as NSString).stringByDeletingPathExtension let fileExt = (fileName as NSString).pathExtension let soundPath = NSBundle.mainBundle().URLForResource(nameOnly, withExtension: fileExt) var player: AVAudioPlayer! = AVAudioPlayer() do { player = try AVAudioPlayer(contentsOfURL: soundPath!, fileTypeHint: nil) } catch let error as NSError { print(error.description) } player.volume = atVolume let playAction: SKAction = SKAction.runBlock { () -> Void in player.play() } if(waitForCompletion){ let waitAction = SKAction.waitForDuration(player.duration) let groupAction: SKAction = SKAction.group([playAction, waitAction]) return groupAction } return playAction } } 
0
source

On performance, it is advisable to convert your audio files to .Wav instead of .mp3. Then, the processor should not decompress the file before using it.

0
source

Source: https://habr.com/ru/post/1264749/


All Articles