var audioPath = NSURL(fileURLWithPath: Bundle.main.path(forResource: "vellipomaakey", ofType: "mp3")!).
fatal error: unexpectedly found nil while unwrapping an Optional value
Did you see it in the Copy Bundle Resources section? If not, press on + sign to add that mp3 file.
You are force unwrapping your optional, consider the following:
if let res = Bundle.main.path(forResource: "vellipomaakey", ofType: "mp3") {
var audioPath = NSURL(fileURLWithPath:res)
}
This will most likely take away your run time error but it won't solve your issue. The problem here is that the resource you are trying to load is not being found, so Bundle.main.path(forResource: "vellipomaakey, ofType: "mp3") is returning nil.
Related
let myPath = Bundle.main.path(forResource: "Settings", ofType: ".png")
print(myPath!)
Why does it crash when I'm trying to print this?
The crash is the famous Unexpected found nil while unwrapping ... error. Don't use exclamation marks unless it's guaranteed that the value is not nil.
Either the file does not exist or (most likely) your type (extension) is png not .png
let myPath = Bundle.main.path(forResource: "Settings", ofType: "png")
However nowadays the URL related API is preferable
let myURL = Bundle.main.url(forResource: "Settings", withExtension: "png")
My simple guess is that myPath is nil, so it crashes at the nil pointer exception. Remove the exclamation mark and use:
print(myPath)
If it prints nil, then you have your answer.
I've had this bug where i use avfoundation to make a path to my audio file so when i use this line of code
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Alone", ofType: "m4r")!))
audioPlayer.prepareToPlay()
It crashes and gives me
fatal error: unexpectedly found nil while unwrapping an Optional value
I have all the code right as the compiler dosen't show any errors.
I am using Xcode 9 and swift 4
This happen because you do forced unwrapping of non-existed path. Generally it's a bad practice. Try to avoid forced unwrapping.
Try this:
guard let path = Bundle.main.path(forResource: "Alone", ofType: "m4r") else {
print("wrong path")
return
}
let url = URL(fileURLWithPath: path)
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.prepareToPlay()
Then if you see a "wrong path" in debug console that means the resource with filename "Alone" and extension "m4r" doesn't exist in your app bundle.
Hope this help.
I ran into an issue when trying to add sound to my game. I added this below to the view controller and when I build and run, I would get an error. When I ran the code below:
var interstitialAd : GADInterstitial!
var player: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "gameMusic.mp3", ofType:"mp3")!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
player = sound
sound.play()
} catch {
print("file not found")
}
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
I got the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
2017-05-21 17:35:01.262683 gameTest2[1660:363851] fatal error:
unexpectedly found nil while unwrapping an Optional value
Can anyone help?
When you use Bundle.main.path(forResource:,ofType:) the first part should be the name of the resource, the second part should be its type. That means your code should be:
let path = Bundle.main.path(forResource: "gameMusic", ofType:"mp3")!
I ve just upgrade from Swift 2 to Swift 3, and i m facing a new challenge...
I have a player which run perfectly before, but now i have this following issue : "unexpectedly found nil while unwrapping an Optional value"
Here is my code :
print(audioselectionne)
let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: audioselectionne as String, ofType: "mp3")!)
I ve got : Optional("tiesto") and the crash...
I really dont understand where is the issue...
Thanks for the help.
You should unwrap the optional, perhaps with optional binding.
BTW, you shouldn't be use path strings at all anymore. Just use the URL directly, e.g.
guard let resource = audioselectionne, let alertSound = Bundle.main.url(forResource: resource, withExtension: "mp3") else {
// handle file not found here
return
}
// use alertSound here
I think the Bundle.main.path method returns an optional String. When that’s nil (because the resource was not found), force-unwrapping it causes your error. If you want to handle it correctly, you have to check for the nil:
guard let path = Bundle.main.path(…) else {
// resource not found, handle error
}
// now `path` is guaranteed to be non-nil
let alertSound = URL(fileURLWithPath: path)
I try to play sound with Swift 2.0
If I write 'try' without '!' I got error
"Errors thrown from here are not handled"
And AVAudioPlayer is not Optional why Xcode request 'try!'
If I write 'try!' my app crash
"unexpectedly found nil while unwrapping an Optional value"
class TouchViewController: UIViewController {
var soundPath:NSURL?
...................
//Play Bipsound
do { soundPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Bipsound", ofType: "wav")!)
var sound = try! AVAudioPlayer(contentsOfURL: soundPath!, fileTypeHint: nil)
sound.prepareToPlay()
sound.play() }
Pretend you've never seen the ! force-unwrapping operator in Swift before and stop using it entirely. It's basically the "Crash if this optional contains nil" operator. Use "if let" style optional binding or try/catch as outlined by #LLooggaann in his excellent answer. (voted)
var soundPath:NSURL?
if let path = Bundle.main.path(forResource: "Bipsound", ofType: "wav") {
soundPath = NSURL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOfURL: soundPath!, fileTypeHint:nil)
sound.prepareToPlay()
sound.play()
} catch {
//Handle the error
}
}