I am trying to load an audio file which is in mp3 format from my project screenshot has been provided from same in which you can see songs.mp3 is present in the porject. When i write below code in my viewcontroller in viewDidLoad func
var player: AVAudioPlayer?
do {
let path = Bundle.main.path(forResource: "songs", ofType: "mp3")
print("path is \(path)")
try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: path!))
player?.play()
} catch let error as NSError {
print(error.description)
}
I'm getting this exception as this is the usual code in Swift for playing audio.
fatal error: unexpectedly found nil while unwrapping an Optional value
2017-10-06 20:04:55.287169+0530 IOSExample[2474:70598] fatal error: unexpectedly found nil while unwrapping an Optional value
How can I play audio file in url?