1

I am trying to play a song based on the instrument the user selects. Here is my code:

@IBAction func play(sender: AnyObject) {

    if isPlaying == false {
        player.play()
        isPlaying = true
    }

}
@IBAction func stop(sender: AnyObject) {

    if isPlaying == true {
        player.stop()
        isPlaying = false
    }

}
@IBAction func pause(sender: AnyObject) {

    if isPlaying == true {
        isPlaying = false
        player.pause()
    }

}



var player = AVAudioPlayer()
var audioPath = NSBundle.mainBundle().pathForResource("Iditarod Bass", ofType: "m4a")

var isPlaying = Bool()

@IBOutlet var instrumentSelect: UISegmentedControl!


@IBAction func didChangeInstrument(sender: AnyObject) {

    if isPlaying == false {

        if instrumentSelect.selectedSegmentIndex == 0 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Bass", ofType: "m4a")

            do {
                try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
            } catch {


            }

        } else if instrumentSelect.selectedSegmentIndex == 1 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Cello", ofType: "m4a")

                do {
                    try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
                } catch {


                }


        } else if instrumentSelect.selectedSegmentIndex == 2 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Viola", ofType: "m4a")
            do {
                try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
            } catch {


            }

        } else if instrumentSelect.selectedSegmentIndex == 3 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Violin 1", ofType: "m4a")

            do {
                try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
            } catch {


            }

        } else if instrumentSelect.selectedSegmentIndex == 4 {

            audioPath = NSBundle.mainBundle().pathForResource("Iditarod Violin 2", ofType: "m4a")

            do {
                try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))

            } catch {


            }


        }
    }

}

All of them work fine except for the instrumentSelect.selectedSegmentIndex == 1. I don't know why. I get an EXC_BAD_INSTRUCTION that says fatal error: unexpectedly found nil while unwrapping an Optional value. The file is working fine, and I have cleaned the project numerous times. I have typed everything correctly. Why is that one not working? I am very confused about this, and I appreciate your help.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
  • 1
    You say "the file is working fine", but clearly not. My guess is that it may be in your _project_ but you have forgotten to add it to your _app target_, so it is not being built into the app. – matt Nov 15 '15 at 19:46
  • Thanks @matt!!! It worked. By working fine, I meant that when I tap the file on the side, it runs. But it wasn't built in to the app. Thanks for your help – Pranav Wadhwa Nov 15 '15 at 21:31

1 Answers1

0

The file was not being added to the app target. It was added to the project only. Thanks @matt for helping me.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61