0

在 gameScene.swift:
声明中:

var backgroundMusic: SKAudioNode!

在 didMoveToView 中:

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

1 回答 1

1

我最终使用 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")
        }
于 2016-04-17T17:06:45.950 回答