【问题标题】:thread 1: exc_bad_instruction (code=exc_i386_invop subcode=0x0) error线程 1:exc_bad_instruction (code=exc_i386_invop subcode=0x0) 错误
【发布时间】:2016-03-03 00:20:59
【问题描述】:

我正在处理这个记录器项目,这段代码是用 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.
}
*/

}

【问题讨论】:

  • 你能把“receivedAudio.filePathUrl”返回的网址贴出来

标签: ios swift


【解决方案1】:

您的问题可能是在分配过程中使用了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 等。

【讨论】:

  • 条件绑定器的初始化器必须有Option类型,而不是'AVAudioPlayer'调用可以trow,但是没有标记'try'并且错误没有处理
  • 我按照你说的做了,还是一样的错误。感谢您的帮助
猜你喜欢
  • 2016-05-13
  • 1970-01-01
  • 2015-10-19
  • 2016-12-06
  • 1970-01-01
  • 2015-05-02
  • 2021-11-03
相关资源
最近更新 更多