为什么swift的路径是零?

时间:2017-10-06 14:39:35

标签: ios swift audio

我正在尝试从我的项目截图中加载一个mp3格式的音频文件,你可以在其中看到songs.mp3存在于项目中。当我在viewDidLoad func

中的viewcontroller中编写下面的代码时
var player: AVAudioPlayer?
do {
    let path = Bundle.main.path(forResource: "songs", ofType: "mp3")
    print("path is \(path)")
    try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: path!))
    player?.play()

} catch let error as NSError {
    print(error.description)
}

我得到了这个例外,因为这是Swift播放音频的常用代码。

  

致命错误:在解开可选值时意外发现nil   2017-10-06 20:04:55.287169 + 0530 IOSExample [2474:70598]致命错误:在解包可选值时意外发现nil

attaching the screenshot of audio file present in project structure

如何在网址中播放音频文件?

0 个答案:

没有答案
相关问题