-2

i'm trying to run this swift code but it outputs the following error: "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)". This is the code:

 //
//  PlaySoundsViewController.swift
//  Revoice
//
//  Created by Leonardo Barazza on 16/04/15.
//  Copyright (c) 2015 Leonardo Barazza. All rights reserved.
//

import UIKit
import AVFoundation

class PlaySoundsViewController: UIViewController {

    var audioPlayer: AVAudioPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        if var filePath = NSBundle.mainBundle().pathForResource("movie_quote", ofType: "mp3") {
            var filePathUrl = NSURL.fileURLWithPath(filePath)
            audioPlayer = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)
        } else {
            println("the filePath is empty")
        }
    }

    @IBAction func playSlowly(sender: UIButton) {
        audioPlayer.play()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

Can you help me?

Thanks in advance.

Leonardo Barazza
  • 375
  • 2
  • 10
  • 2
    you may want to give more information where exactly the debugger breaks. and gives you the error. In general, a nil is given where your code expected a non nil. – Volker Apr 19 '15 at 14:00
  • Do not explicitly unwrap things that can ever, under any circumstances be nil–unless you want crashes. – zaph Apr 19 '15 at 14:44
  • possible duplicate of [fatal error: unexpectedly found nil while unwrapping an Optional value](http://stackoverflow.com/questions/24948302/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-value) – GoZoner Apr 19 '15 at 16:03

2 Answers2

1

I guess the problem is in audioPlayer property: it's defined as an implicitly unwrapped AVAudioPlayer optional, so it cannot be nil whenever you access it (otherwise a runtime exception is thrown, because of the implicitly unwrap).

The problem is that you actually create an instance (audioPlayer != nil) only if filePath != nil. Did you check that filePath is not nil?

By the way you can skip this problem by using optional chaining:

@IBAction func playSlowly(sender: UIButton) {
    if audioPlayer?.play() == nil {
        println("audioPlayer is nil")
    }
}
user2340612
  • 10,053
  • 4
  • 41
  • 66
0

From your code above the question, you created only one instance variable, audioPlayer: AVAudioPlayer , with excalmatory mark !.

  • The error will show when the complier use the instance which was not created yet.
  • After read your code, only one place used the instance of AVAudioPlayer, func playSlowly(). But you did not show your debugger information, I can just guess audioPlayer.play() is the place where the error is.
  • I think the code if var filePath = NSBundle.mainBundle().pathForResource("movie_quote", ofType: "mp3") , leads to the failing to define the instance of audioPlayer.
  • You should check whether the definition of filePath is correct.
Yichen Zhou
  • 436
  • 6
  • 21