我使用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)
}
答案 0 :(得分:2)
在转到下一个场景之前停止AVAudioPlayer
在您的segue代码
之前插入此内容audioPlayer.pause()