如何解决线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子码=为0x0)

如何解决线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子码=为0x0)

问题描述:

我想打在我的项目的音频在做IOS(SWIFT语言)。我不能播放音频因错误如何解决线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子码=为0x0)

线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子码=为0x0)

我能起到同样的音频时不将其分离。但是,当我将它添加到我的项目获取上述错误。我尝试用不同的代码播放音频。但同样的错误在同一行中重复出现。

@IBAction func playaudio(sender:AnyObject) 
    { 
    var ButtonAudioPlayer: AVAudioplayer! 
    let path = NSBundle.mainBundle()pathResource("ButtonAudio.wav",ofType:nil)! // error 
    let url = NSURL(fileURLWithPath: path) 
    do 
    { 
     let ButtonAudioPlayer1 = try! AVAudioPlayer(contesOfURL:url) 
     ButtonAudioPlayer = ButtonAudioPlayer1 
     ButtonAudioPlayer1.play() 
    } 
} 

你得到EXC_BAD_INSTRUCTION 到期强行展开一个零路径。检查文件的路径并使用ofType中的“wav”。这样的代码将帮助你解决这个问题:

guard let path = NSBundle.mainBundle().pathForResource("ButtonAudio", ofType: "wav") else { 
    print("there is not such a file") 
    return 
} 
+0

我试过这种方式。但我仍然在同一行中得到同样的错误。 – sitara

+0

尝试使用'URLForResource(_:withExtension :)'并让我知道它是否会改变任何东西。 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/#//apple_ref/occ/instm/NSBundle/URLForResource:withExtension: –

+0

始终打印“没有这样的文件“ – sitara

在这里我注意到了一些其他项目。因为它缺少

你的路径上分配不正确。在pathForResource之前。如上所述,你也错过了这种类型。

当分配buttonAudioPlayer,你有一个错字,你缺少的contentsOfUrl n个。

您还没有使用驼峰你的变量,这是使用常见的做法。

@IBAction func playAudio(sender:AnyObject) { 
    var buttonAudioPlayer: AVAudioPlayer 
    if let path = NSBundle.mainBundle().pathForResource("ButtonAudio", ofType: "wav") { 
     let url = NSURL(fileURLWithPath: path) 
     do { 
      let buttonAudioPlayer1 = try! AVAudioPlayer(contentsOfURL: url) 
      buttonAudioPlayer = buttonAudioPlayer1 
      buttonAudioPlayer.play() 
     } 
    } 
} 

尝试这样,

// Grab the path, make sure to add it to your project! 
var path = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("ButtonAudio", ofType: "wav")) 
var audioPlayer = AVAudioPlayer() 

audioPlayer = AVAudioPlayer(contentsOfURL: path, error: nil) 
audioPlayer.prepareToPlay() 

audioPlayer.play() 

,并确保您已在主束添加音频正常。打印它是你得到的路径。