我正在尝试在按下按钮时播放MP3文件(在VLC / iTunes中播放正常)。 这是我的代码:
var audioPlayer: AVAudioPlayer!
@IBAction func playEpisode(sender: AnyObject) {
println("now playing")
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
let data: CDEpisode = fetchedResultsController.objectAtIndexPath(indexPath!) as! CDEpisode
var err: NSError?
let url = NSURL(string: data.localPath)
println("The url is \(url)")
audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &err)
if audioPlayer == nil {
if let e = err {
println(e.localizedDescription)
}
}
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
audioPlayer.play()
}
这里是日志记录:
now playing
The url is Optional(file:///var/mobile/Containers/Data/Application/4747A71E-A63F-4EFC-B2DF-8B361361080B/Documents/serial-s01-e12.mp3)
The operation couldn’t be completed. (OSStatus error 2003334207.)
fatal error: unexpectedly found nil while unwrapping an Optional value
在audioPlayer.delegate = self
时会发生EXC_BREAKPOINT
错误。
其他StackoOverflow上的线程没有帮助。有什么想法吗? 谢谢
编辑:我尝试将localURL传递给contentsOfURL(而不是CDEpisode对象),但仍然失败。