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