当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")
        }

相关内容

最新更新