gpt4 book ai didi

ios - 线程 1 : exc_bad_instruction (code=exc_i386_invop subcode=0x0) error

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

我正在开发这个记录器项目,这段代码是用 swift 2.0 编写的,它给出了这个问题!我有类似的标题帖子,但与我遇到的问题无关

导入UIKit导入AVFoundation

类 PlaySoundViewController:UIViewController {

var audioPlayer: AVAudioPlayer!
var receivedAudio: AudioRecorded!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.




do{

let audioPlayer = try AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl) --> ***The error happens here***

audioPlayer.enableRate = true
}catch let ErrorType {

NSLog("Could not create AVAudioPlayer: \(ErrorType)")
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.

}

@IBAction func playFastSound(sender: AnyObject) {
audioPlayer.stop()
audioPlayer.play()
audioPlayer.rate = 2.0



}

@IBAction func playSlowSound(sender: AnyObject)
{
audioPlayer.stop()
audioPlayer.play()
audioPlayer.rate = 0.5

}


@IBAction func stopPlaying(sender: AnyObject) {

audioPlayer.stop()

}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

最佳答案

您的问题可能是在分配期间使用 try!

HWS puts it -

There's one more thing to discuss, which is what to do if you know a call simply can't fail, for whatever reason. Now, clearly this is a decision you need to make on a case-by-case basic, but if you know there's absolutely no way a method call might fail, or if it did fail then your code was so fundamentally broken that you might as well crash, you can use try! to signal this to Swift.

所以这可能不是您想要的。对于错误处理,您通常会使用 try(不带 !)。

    do {
let audioPlayer = try AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl)
// Do stuff with audio player
}
catch let error {
NSLog("Could not create AVAudioPlayer: \(error)")
}

现在这并不能解释为什么没有创建音频播放器。最有可能的是 URL 无效,它指向的文件不存在,或者它的编码方式是 AVAudioPlayer 无法读取的。通常您需要标准文件格式,例如 mp3、mp4 等。

关于ios - 线程 1 : exc_bad_instruction (code=exc_i386_invop subcode=0x0) error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760844/

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