当 gameScene 首先加载时工作,但播放半秒并在被另一个场景加载时立即停止(无错误),为什么?

works when gameScene is loaded first , but play for half a sec and immediately stops (no error) when loaded by another scene, why ?

在 gameScene.swift 中:
声明:

var backgroundMusic: SKAudioNode!

在 didMoveToView 中:

  if let musicURL = NSBundle.mainBundle().URLForResource("Jalandhar", withExtension: "mp3") {
            backgroundMusic = SKAudioNode(URL: musicURL)
             addChild(backgroundMusic)
        }

我最终使用 AVAudioPlayer 来解决它,代码可能有用:
声明为 属性 :

var audioPlayer: AVAudioPlayer!

然后在函数中:

 let filePath = NSBundle.mainBundle().pathForResource("musicfile", ofType: "mp3")
        let url = NSURL.fileURLWithPath(filePath!)
        do {
            audioPlayer = try AVAudioPlayer(contentsOfURL: url, fileTypeHint: nil)
            audioPlayer.play()        }
        catch {
            print("music error")
        }