线程1:exc_bad_instruction(code = exc_i386_invop subcode = 0x0)错误

时间:2016-03-03 00:20:59

标签: ios swift

我正在研究这个录像机项目,这段代码是用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.
}
*/

}

1 个答案:

答案 0 :(得分:0)

您的问题可能是在分配期间使用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等。