【发布时间】:2015-05-19 06:17:17
【问题描述】:
按下按钮时,我正在尝试播放 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
EXC_BREAKPOINT 发生在 audioPlayer.delegate = self 上。
StackoOverflow 上的其他线程没有帮助。有任何想法吗? 谢谢
编辑:我尝试将 localURL 传递给 contentsOfURL(而不是 CDEpisode 对象),但仍然失败。
【问题讨论】:
-
我查看了错误代码,2003334207 给出了本地化描述“操作无法完成”。在我的应用中,我试图访问一个不存在的文件。
标签: ios swift avaudioplayer