Swift SpriteKit在后台播放音频

Cho*_*ain 3 ios swift

我有一个应用程序,需要在后台播放音频...这可能使用Swift和SpriteKit与SKActions ...或者它可能是另一种方式..

向正确的方向轻推会非常有帮助.

lch*_*amp 6

SKAction 用声音很容易使用,但有时你可能想做更多.

在这种情况下,您可能希望使用AVAudioPlayer而不是它.

为了不写自己的"播放器",我建议你使用现有的"播放器".这是我已经使用过的一个(SKTAudio):https://github.com/raywenderlich/SKTUtils/blob/master/SKTUtils/SKTAudio.swift

以下是如何使用它:

// 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
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,您将能够播放短音(就像您可能已经使用过的那样SKAction),甚至可以播放将要播放的背景音乐.