Audio is not working.

Hi, I am trying to incoporate audio into an app that I'm making and I use the simulator to test but the second it gets on it goes back to the project screen with a line highlighted and a thread error below it saying

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

and another error in the debug area saying

fatal error: unexpectedly found nil while unwrapping an Optional value  (lldb)

The code i am using is this


/
  ViewController.swift
  ***********************
  Created by Marco Simonelli on 26/04/2015.
  Copyright (c) 2015 Antonio Enterprises. All rights reserved.
*/
import UIKit
import AVFoundation
class ViewController: UIViewController {
   
   
    var  ButtonAudioURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("TURDY BIRD BACKGROUND T1 copy", ofType: "wav")!)
   
    var ButtonAudioPlayer = AVAudioPlayer()
   
    override func viewDidLoad() {
        super.viewDidLoad()
        /
       
       ButtonAudioPlayer = AVAudioPlayer(contentsOfURL: ButtonAudioURL, error: nil)
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
   
    @IBAction func PlayButton1(sender: AnyObject) {
    }
}

line 20 is where i get the thread error.


please help me.

Thanks in advance.

🙂🙂🙂


P.S.

i am not sure if i was meant to add in a library or emmbedded binarys in it beause i have not.

Accepted Answer

> fatal error: unexpectedly found nil while unwrapping an Optional value


This is a pretty straightforward Swift runtime error: it means that Swift has tried to get the value out of an Optional value and got nil instead. Given the code you posted it's likely that this is related to ButtonAudioURL, probably because the file simply isn't available in your bundle.


btw Constructing a file URL from a path from a bundle is weird. Why not use one of NSBundle -URLForResource:*** calls?


Share and Enjoy

--

Quinn "The Eskimo!"

Apple Developer Relations, Developer Technical Support, Core OS/Hardware

thank you so much!!!

so it would be

NSBundle -URLForResource:MySong.mp4

Right?

Audio is not working.
 
 
Q