【问题标题】:Does anybody know how to fix Do-Try-Catch error code= EXC_1386_INVOP in swift?有谁知道如何快速修复 Do-Try-Catch 错误代码 = EXC_1386_INVOP?
【发布时间】:2017-08-17 21:30:41
【问题描述】:

当我尝试添加音频文件时,我正在快速编写计算器应用程序,我将其放在 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()
    }

}

【问题讨论】:

  • auudioPath 为零吗?如果是这样,则 mp4 文件实际上不在您的应用程序包中。
  • try player = 是否编译?顺便说一句,为什么不Bundle.main.url(forResource: "song", withExtension: "mp4")
  • audiopath 等于 nil,但 mp4 是我的文件类型

标签: swift error-handling


【解决方案1】:

您正在检索路径而不是 URL...其次您的尝试语法错误...尝试此代码

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

【讨论】:

    猜你喜欢
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    相关资源
    最近更新 更多