Does the sound causing the game lag behind in a fast game with a set of sprites?

New code

class SceneTwo: SKScene, SKPhysicsContactDelegate { let flap = SKAction.playSoundFileNamed("flap.caf", waitForCompletion: false) let whack = SKAction.playSoundFileNamed("whack.caf", waitForCompletion: false) let tap = SKAction.playSoundFileNamed("tap.caf", waitForCompletion: false) 

Then I just put

Run (tap) run (flap), etc. where is it necessary ..

Hi, it’s just interesting if I use the correct coding to play sounds in my game. In some context, my game is like a Fluffy bird. One sound is played every time you touch the screen (when the bird has an impulse up), the second sound - when the bird collects a coin between each wall.

I noticed that both of these sounds make my game lag.

Below is my relative sound code for the game.

 import AVFoundation var flap: AVAudioPlayer? var tap: AVAudioPlayer? override func didMove(to view: SKView) { tap?.prepareToPlay() flap?.prepareToPlay() func playFlap() { let url = Bundle.main.url(forResource: "flap", withExtension: "caf")! do { flap = try AVAudioPlayer(contentsOf: url) guard let flap = flap else { return } flap.play() } catch let error { print(error.localizedDescription) } } func playTap() { let url = Bundle.main.url(forResource: "tap", withExtension: "caf")! do { tap = try AVAudioPlayer(contentsOf: url) guard let tap = tap else { return } tap.play() } catch let error { print(error.localizedDescription) } } 

After that i just

 playTap() playFlap() 

to where they are needed.

The sound is clear, it seems that my spawning walls are slightly accelerated when the sound is made. Is there something I am doing is wrong?

0
source share
1 answer

You get lag because you do not pre-load sound files. You can preload them in the App Launch, and then when you just need to play them. Check out this fooobar.com/questions/1264749 / for reference .

And if you still face the same problem, you can add sound in the background as shown here

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

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


All Articles