有人知道如何在swift中修复Do-Try-Catch错误代码= EXC_1386_INVOP吗?

时间:2017-08-17 21:30:41

标签: swift error-handling

当我尝试添加音频文件时,我正在swift中编写计算器应用程序我把它放在calc编程之上:

var player:AVAudioPlayer = AVAudioPlayer()

@IBAction func play(_ sender: UIButton)
{
    player.play()
}

并且在视图下方确实加载我把它。拜托,任何帮助都会很棒!

override func viewDidLoad() {
    super.viewDidLoad()
    //Do any additional setup after loading the view, typically from a nib.
    do
    {
        let audioPath = Bundle.main.path(forResource: "song", ofType: "mp4")
        try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
    }
    catch
    {
        // error
        abort()
    }

}

1 个答案:

答案 0 :(得分:0)

你正在转发路径不是网址...其次你尝试语法错误...试试这段代码

 do
{
    let audioPath = Bundle.main.url(forResource: "song", withExtension: "mp4")
    player = try AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
}
catch
{