在游戏场景中。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")
}