-1

I get an error every time I try to run stating

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

Could someone explain why? heres the code

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player: AVAudioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()

        let audioPath = NSBundle.mainBundle().pathForResource("Belly - Might Not", ofType: "mp3")!

        do {
           try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))
           player.play()
        } catch {
            // Process error here
        }
    }
}
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
D.watts
  • 9
  • 1
  • Your nil is likely coming from the unwrapping of pathForResource when assigning to audioPath. Did you double check the path for audioPath. Is your mp3 included in your project? – buczek Mar 25 '16 at 16:39
  • Yes the Mp3 is included. It just cant find the path – D.watts Mar 25 '16 at 16:42

3 Answers3

0

This error is almost always caused by force unwrapping an object, i.e. the "!"operator. for your code, it's likely this line:

let audioPath = NSBundle.mainBundle().pathForResource("Belly - Might Not", ofType: "mp3")!

Chances are t can't find that file. To be safe with it and handle this error case, use this:

if let audioPath = NSBundle.mainBundle().pathForResource("Belly - Might Not", ofType: "mp3") {
    /* do what you need to with the path*/
 }
else{
    /* handle error case */
}
GetSwifty
  • 7,568
  • 1
  • 29
  • 46
0

You're force-unwrapping the optional in your line of code:

let audioPath = NSBundle.mainBundle().pathForResource("Belly - Might Not", ofType: "mp3")!

This file can returns an optional in case of not exist the resource, avoid the force unwrapping of the optional instead use optional-binding or a guard statement like in the following way. It's always recommend not make force-unwrapping of an optional because you're telling to the compiler that you know that always be different of nil and if it's happen you get an runtime error.

if let audioPath = NSBundle.mainBundle().pathForResource("Belly - Might Not", ofType: "mp3") {
   do {
       try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))
       player.play()
    } catch {
        // Process error here
    }
}

Or with guard:

guard let audioPath = NSBundle.mainBundle().pathForResource("Belly - Might Not", ofType: "mp3") else { return }

do {
     try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))
     player.play()
} catch {
   // Process error here
}

I hope this help you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • For some reason the code works that you provided but I cant get the music to play in the simulator when I run it. Does the simulator have a sound of its own or is it just with the Mac itself. – D.watts Mar 25 '16 at 16:32
  • 1
    @D.watts My answer solve your question for more information about your other error I recommend you http://stackoverflow.com/questions/24962822/why-doesnt-ios-system-sound-play-in-simulator or http://stackoverflow.com/questions/30986446/ios-swift-sound-not-playing-on-iphone-simulator – Victor Sigler Mar 25 '16 at 16:36
  • Ok thanks for your help. In the code you provided. It printed path not found, Im guessing that indicates that it couldnt find the path. would that mean the source im using is bad? – D.watts Mar 25 '16 at 16:38
  • You're welcome :). Yes it could be, you need to be careful where you put the file and be careful with the name too. Please if the answer solves your question accept it to it can help someone else – Victor Sigler Mar 25 '16 at 16:41
0

May be audio file not found. Try like this

if let audioPath = NSBundle.mainBundle().pathForResource("Belly - Might Not", ofType: "mp3"){

        do {
          try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))
          player.play()
        } catch {
          // Process error here
        }

      }else{
        print("path not found")
      }
Muzahid
  • 5,072
  • 2
  • 24
  • 42