-3

I am fairly new to this and trying to figure out the correct format to solve the error in the title. I get on the line: let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil)!

I know I must be missing something just not sure where.

import UIKit import AVFoundation

class ViewController: UIViewController {

@IBOutlet var playButton: UIButton!

var playPug = 1

var player: AVAudioPlayer!

@IBAction func playPressed(sender: AnyObject) {


    let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil)!

    let url = NSURL(fileURLWithPath: audioPath)

    do  {

        if playPug == 1 {
            let sound = try AVAudioPlayer(contentsOfURL: url)
            player = sound
            sound.play()
            playPug = 2
            playButton.setImage(UIImage(named:"pause_Icon.png"),forState:UIControlState.Normal)
        } else {
            player.pause()
            playPug = 1
            playButton.setImage(UIImage(named:"play_Icon.png"),forState:UIControlState.Normal)
        }

    } catch {
        print(error)
    }

}
Lou
  • 99
  • 1
  • 1
  • 8
  • 2
    If you look at Apple's documentation you will see that the method is `init(contentsOfURL url: NSURL)` and uses Swift 2 error handling. The documentation should always be your first place to look. Link to `AVAudioPlayer` documentation: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/#//apple_ref/occ/instm/AVAudioPlayer/initWithContentsOfURL:error: . Link to Swift Error Handling documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html – Robotic Cat Jul 29 '16 at 17:34
  • 1
    Thanks, I see the init(contentsOfURL url: NSURL) method example. I am still not sure how to change it though. I have never seen "throw" before.. – Lou Jul 29 '16 at 17:48
  • SO is not a good place for tutorials. I suggest you do an internet search for a tutorial on Swift error handling. Example: https://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch – Robotic Cat Jul 29 '16 at 19:58
  • Thanks, helpful! I have updated the code and it works except for a fatal error i am trying to work out now... – Lou Jul 29 '16 at 20:39
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Robotic Cat Jul 29 '16 at 22:18

1 Answers1

1

The reason you're getting fatal error: unexpectedly found nil while unwrapping an Optional value is because of the ! in this line of code:

let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil)!

It's crashing because you're using ! to force unwrap the value returned by pathForResource(_:ofType:), which is unsafe. If the value is nil, you get the unexpectedly found nil error. You should really only force unwrap things when you know they're not going to be nil.


Try doing something like this instead:

Option 1:

guard let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil) else {
    // The resource does not exist, so the path is nil.
    // Deal with the problem in here and then exit the method.
}

// The resource exists, so you can use the path.

Option 2:

Use optional binding, like this:

if let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil) {

    // The resource exists, and now you have the path, so you can use it.

    let url = NSURL(fileURLWithPath: audioPath)

    do {

        if playPug == 1 {
            let sound = try AVAudioPlayer(contentsOfURL: url)
            player = sound
            sound.play()
            playPug = 2
            playButton.setImage(UIImage(named:"pause_Icon.png"),forState:UIControlState.Normal)
        } else {
            player.pause()
            playPug = 1
            playButton.setImage(UIImage(named:"play_Icon.png"),forState:UIControlState.Normal)
        }

    } catch {
        print(error)
    }

} else {
    // The path was nil, so deal with the problem here.
}
Søren Mortensen
  • 1,663
  • 1
  • 11
  • 25