线程1:exc_bad_instruction(代码= exc_i386_invop子代码= 0x0)错误
我正在研究这个记录器项目,这个代码是用swift 2.0编写的,它给出了这个问题! 我似乎类似的标题文章,但不涉及我有线程1:exc_bad_instruction(代码= exc_i386_invop子代码= 0x0)错误
进口的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 -
有一两件事来讨论,这是做什么,如果你知道一个电话根本不能失败,无论出于何种原因。现在,显然这是您需要根据具体情况做出的基本决定,但是如果您知道方法调用绝对没有办法可能会失败,或者如果它失败了,那么您的代码就会被彻底打破,以至于您可能会以及崩溃,你可以使用尝试!向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等标准文件格式。
我尝试了,现在我又出现了两个错误: – user2074226
有条件粘合剂的初始化程序必须有选项类型,而不是'AVAudioPlayer'呼叫可以缩小,但不会标记为'尝试'并且不处理错误 – user2074226
@ user2074226 - 答复已更新。 – mszaro
你能发布为“receivedAudio.filePathUrl”返回的网址吗? –