Why Is My Sound Making My Game Lag in Swift Spritekit

Why is my sound making my game lag in Swift Spritekit?

Use SKAction.playSoundFileNamed. When you create an SKAction instance ahead of time, it does all the necessary prep to perform the action (in this case, playing a sound) without lag during gameplay. To run the action (play the sound), call runAction on a node — this can be any node, even the scene itself.

Since it doesn't matter which node you use for sound purposes, use whichever is most convenient. For example, if you're just playing a sound you might call runAction on the scene. But if your sound is part of an action group or sequence that, say, animates a sprite, you could make the sound action part of that sequence and play it on the sprite that you're animating.

See SpriteKit Programming Guide for more about actions.


Unrelated Swift tip: use let instead of var for references that won't change. It can help keep you from introducing bugs later, and it probably helps the compiler optimize your code, too.

Sound causing game to lag in swift sprite kit game?

You are getting lag because you are not preloading the sound files. You can preload them at App Launch, and then when you need just play them. For reference look into this stackoverflow's post

And if you still face the same issue then you can add sound in background queue, as demostrated here

let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
audioPlayer.play()
})

Can't understand the lag in my spritekit game?

If you want to use AVAudioPlayer (instead of SpriteKit's sound actions), you should make the sounds play in a background thread so that its operation won't interfere with the main thread where all of your visual stuff goes:

 let soundQueue = OperationQueue()
soundQueue.qualityOfService = QualityOfService.background
soundQueue.addOperation{self.buttonClickSound.play()}

Why is there a lag in my game everytime my sound is being played in swift?

lower the quality/bitrate of the audio that you play.

AVAudioPlayer and performance issue in SpriteKit game

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.

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()

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

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.

Hope this helps

Swift game lags when creating spritenodes

Your approach sounds very similar to a SpriteKit tutorial I wrote, but I had no lag at all there. SpriteKit is awfully fast, but it's possible your exact conditions are causing problems. Things to check:

1) Are you using texture atlases? These are faster to load than separate images in your bundle.

2) If you're re-using images a lot, can you store them in an SKTexture then load your nodes from there?

3) Are there other things happening, such as sounds playing? The iOS Simulator frequently has a brief lag when it plays its first sound in a game.

4) Is the path that gets created very complicated?

AVAudioPlayer produces lag despite prepareToPlay() in Swift

I ran into this same problem and played the sound in the backgroundQueue.

This is a good example: https://stackoverflow.com/a/25070476/586204.

let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
audioPlayer.play()
})


Related Topics



Leave a reply



Submit