Swift SpriteKit playing audio in the background

2020-06-23 11:31发布

I have an app which needs to play audio in the background... Is this possible using Swift and SpriteKit with SKActions... Or is it possible another way..

A nudge in the right direction would be very helpful.

标签: ios swift
2条回答
够拽才男人
2楼-- · 2020-06-23 11:55

After trying out a few things I stumbled upon a reasonable solution. For playing music in a loop you can set up an SKAudioNode and set the AutoPlayedLoop = true. Then call the "play music" method in when Scene loads.

Now, if you want a sound effect you can use an SKAudioNode for that as well. I found that the sound effect started repeating and so to counteract that I simply created a play-stop sequence. Essentially, every time the sound effect was triggered it would run this sequence, hence, it only sounded once, as intended. There was a cut off to the sound effect however, so I created a simple colour shade method for about 1 second and added this to the sequence. Now the sequence produced was play-shade-stop. Every time the sound effect ran this sequence the dormant shade action would give it a 1 second lag and allow it to play fully and then it would stop as intended. Based on the sound effect you can use the dormant shade action to compensate as required, sometimes longer, sometimes shorter.

Well, here is the code. It's pretty straightforward and you don't need to create a whole AVPlayer class for it.

func playerhitfx() {
    let fx:SKAudioNode = SKAudioNode(fileNamed: "playerfx")
    let play: SKAction = SKAction.play()
    let stop: SKAction = SKAction.stop()
    let shade: SKAction = SKAction.colorize(with: UIColor.clear, colorBlendFactor: 1, duration: 1)
    let volume: SKAction = SKAction.changeVolume(to: 3, duration: 0)
    let seq: SKAction = SKAction.sequence([play,shade,stop])
    fx.run(seq)
    fx.run(volume)
    self.addChild(fx)


}

Furthermore, you can adjust the volume properties having all audio as AudioNodes, to create a good audio mix between the music in the background and the sound effects and also to pronounce certain other effects over others. Hope this helps.
By the way, you can't cast an SKAudioNode into an AVAudioNode, just to bare in mind.

查看更多
混吃等死
3楼-- · 2020-06-23 11:57

SKAction is really easy to use with sounds, but sometimes you might want to do more.

In that case, you would want to use AVAudioPlayerinstead of it.

In order to not write your own "player", I suggest you to use an existing one. Here is one I've already used (SKTAudio) : https://github.com/raywenderlich/SKTUtils/blob/master/SKTUtils/SKTAudio.swift

Here is how to use it :

// For background audio (playing continuously)
SKTAudio.sharedInstance().playBackgroundMusic("yourBackgroundMusic.mp3") // Start the music
SKTAudio.sharedInstance().pauseBackgroundMusic() // Pause the music
SKTAudio.sharedInstance().resumeBackgroundMusic() // Resume the music

// For short sounds
SKTAudio.sharedInstance().playSoundEffect("sound.wav") // Play the sound once

As you can see, you'll be able to either play short sound (as you might already have done with SKAction) and even background music that will play in loop as you're looking for.

查看更多
登录 后发表回答