您好我正嘗試在swift 2中播放帶有以下代碼的音樂文件。基本上我只是用名字f.mp3到asses文件夾,並且我的代碼中斷了以下消息: 意外地發現零,同時展開可選值。在哪裏我需要把我的MP3文件,所以IOS可以找到它。謝謝致命錯誤:在Swift 2中使用AudioPlayer時解開可選值時出現意外的零錯誤
var audioPlayer: AVAudioPlayer! = nil
func playMyFile() {
let path = NSBundle.mainBundle().pathForResource("f", ofType: "mp3")
let fileURL = NSURL(fileURLWithPath: path)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: fileURL)
} catch {
print("error")
}
audioPlayer.prepareToPlay()
audioPlayer.delegate = self
audioPlayer.play()
}
可能重複(http://stackoverflow.com/questions/32170456/what-does- fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – jtbandes