离开GameScene时停止SpriteKit中的音乐

时间:2016-12-14 21:02:52

标签: ios swift sprite-kit avfoundation

我使用AVFoundation在游戏中实现了背景音乐。然而,当离开GameScene时,音乐继续播放。我知道在viewControllers中你可以使用viewWillDisappear但我找不到任何类似的GameScenes。

var audioPlayer = AVAudioPlayer()
let audioPath = Bundle.main.path(forResource: "electroDrive", ofType: "wav")

//.......//

//.......//

    do {

        try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))

    }
    catch {
        // process error

    }

    audioPlayer.play()


//Code to go to other scene
if viewController != nil {
        self.viewController?.performSegue(withIdentifier: "push", sender: viewController)
    }

1 个答案:

答案 0 :(得分:2)

在转到下一个场景之前停止AVAudioPlayer

在您的segue代码

之前插入此内容
audioPlayer.pause()