0

I am trying to play an audio in my project done in ios (swift language) .I can't play the audio due to the error

thread 1:EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,subcode=0x0)

I can play the same audio when do it separate. But when I add it to my project getting the above mentioned error. I tried with different codes for playing audio. But the same error is repeating in the same line.

  @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()
    }
}
Idan
  • 5,405
  • 7
  • 35
  • 52
sitara
  • 61
  • 11

3 Answers3

2

You are getting EXC_BAD_INSTRUCTION due to force unwrapping a nil path. Check path for your file and use "wav" in ofType. Code like this will help you figure this out:

guard let path = NSBundle.mainBundle().pathForResource("ButtonAudio", ofType: "wav") else {
    print("there is not such a file")
    return
}
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
Tomasz Bąk
  • 6,124
  • 3
  • 34
  • 48
  • I tried this way. But I still get the same error in the same line. – sitara May 31 '16 at 05:58
  • Try using `URLForResource(_:withExtension:)` and let me know if it changes anything. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/#//apple_ref/occ/instm/NSBundle/URLForResource:withExtension: – Tomasz Bąk May 31 '16 at 06:04
  • always printing "there is not such a file" – sitara May 31 '16 at 07:02
  • So problem is that the wav file is not in the app's bundle or is inside a directory. Experiment with 'URLForResource:withExtension:subdirectory:' – Tomasz Bąk May 31 '16 at 08:11
1

There were a few other items in here I noticed.

Your assignment on the path is incorrect because it is missing the . before pathForResource. As noted, you also missed the type.

When you assign your buttonAudioPlayer, you have a typo where you are missing the n in contentsOfUrl.

You are also not using camelCase with your variables, which is common practice to use.

@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()
        }
    }
}
CodeBender
  • 35,668
  • 12
  • 125
  • 132
0

Try like this,

// 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()

And make sure that you have added audio properly in main bundle. print it's path which you get.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75