-1

I am getting this problem in my code:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

This message appears on this line of my code:

try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation!))

Any help would be greatly appreciated. Thanks in advance!

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {

        if event?.subtype == UIEvent.EventSubtype.motionShake {

            let fileLocation = Bundle.main.path(forResource: "sound1", ofType: "mp3")

            do {
                try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation!))
                player.play()
            } catch {
                // process error   
            }   
        }   
    }
}
Arc676
  • 4,445
  • 3
  • 28
  • 44
J.BAT
  • 99
  • 2
  • 9
  • That error is basically telling you that `fileLocation` is `nil` when it shouldn't. – grooveplex Jan 31 '19 at 18:33
  • ok thanks! So how can I fix this? – J.BAT Jan 31 '19 at 18:35
  • @grooveplex did I do something wrong? – J.BAT Jan 31 '19 at 18:36
  • You need to assure that `fileLocation` is not `nil`, which probably means making sure the file you're trying to load exists — I can't help you much more, because I'm not very experienced with swift yet – grooveplex Jan 31 '19 at 18:38
  • @grooveplex ok thanks anyway! – J.BAT Jan 31 '19 at 18:39
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Cristik Jan 31 '19 at 20:33

1 Answers1

0

Your file wasn't found.

I made some minor changes to aid debugging and prevent crashing.

import UIKit
import AVKit

class ViewController: UIViewController {

    var player = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {

        if event?.subtype == UIEvent.EventSubtype.motionShake {
            if let fileLocation = Bundle.main.path(forResource: "sound1", ofType: "mp3") {
                do {
                    let player = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation))
                    player.play()
                } catch (_) {
                    print("AVAudioPlayer could not play the file.")
                }
            } else {
                print("File was not be found.")
            }
        }
    }
}
Maximilian
  • 463
  • 5
  • 16
  • Thanks for helping! I tired the code but It keeps saying "File was not be found." – J.BAT Jan 31 '19 at 18:59
  • Where is sound1.mp3 located? I placed it in the root directory of the project and it worked for me! – Maximilian Jan 31 '19 at 19:07
  • hey I put it in the root folder but inside another folder called “sounds” – J.BAT Jan 31 '19 at 19:08
  • Have you tried prepending the folder name? `Bundle.main.path(forResource: "sounds/sound1", ofType: "mp3")` – Maximilian Jan 31 '19 at 19:18
  • I’ve tired that too multiple times but not having any luck – J.BAT Feb 01 '19 at 00:16
  • I think you need to use func `url(forResource:wihExtension:subdirectory:)` on Bundle.main [link](https://developer.apple.com/documentation/foundation/bundle/1416712-url) You can then pass "sounds" as the subdirectory. – Jonathan Feb 14 '19 at 17:58