gpt4 book ai didi

swift - 有谁知道如何快速修复 Do-Try-Catch 错误代码= EXC_1386_INVOP ?

转载 作者:行者123 更新时间:2023-11-30 12:13:25 24 4
gpt4 key购买 nike

当我尝试添加音频文件时,我正在快速编写计算器应用程序,我将其放在计算编程之上:

var player:AVAudioPlayer = AVAudioPlayer()

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

在下面的 View 中确实加载了我放置的这个。请提供任何帮助!

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()
}

}

最佳答案

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

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

关于swift - 有谁知道如何快速修复 Do-Try-Catch 错误代码= EXC_1386_INVOP ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45745233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com