3

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

attaching the screenshot of audio file present in project structure

How can I play audio file in url?

  • nope here AV audio player object is coming as nil. so far i know "?" signify optional value i m getting nil at player?.play() line of code which clear point out the path is coming as nil. my question is why the path is coming as nil –  Oct 06 '17 at 14:45
  • is there no standards for checking duplicate question in stackoverflow. How on this earth audip player randomly get duplicated becoz of some other questions. nil is generic error it can comes in nth times in programmes that doesnt mean its duplicate. How can u mark the duplicate without a justification why path is nil??? –  Oct 06 '17 at 14:48
  • didnt u read the title of the question before writing the comment ? if no pls read it. Question has image which show mp3 file present in project the code is pasted which is written. if u dont know the answer u dont have write to mark the question. It seen u dont have useful info to give from developer point of view. If u dont understand anything pls ask show some etiquette .question was to about why audio is not playing . writing in bold "WHY AUDIO IS NOT PLAYING AND FILE IS NOT GETTING PICKED FROM PROJECT DIRECTORY " there is no answer from ur side. –  Oct 06 '17 at 15:39
  • can u pls explain what is "useful information" which is missing. Be logical, i didnt understand what i missed in the question i f u can comment earlier that these info is required for further investigation which is missing i would have provided it. If information is missing how can u mark it as duplicate of any irrelevant question without asking the details of questions. If u guys dont wanna answer it. its fine dude n pls be gentle to the person who is asking the questions. dont demotivate this forum. This forum is used for improving our skills and understanding. Which is what we do here –  Oct 06 '17 at 15:53
  • question has been updated pls check –  Oct 06 '17 at 16:04
  • if i would love to answer what u r asking again highlighting the fact "you still make no mention of the fact that path is nil" question title says why path is coming nil. i m asking that question how can i answer this if i can answer this why the hell i will write this question ? –  Oct 06 '17 at 16:11
  • 1
    Have you double-checked that the "target membership" checkbox is selected for the "songs.mp3" file? Otherwise it won't be copied to the application bundle. – Martin R Oct 06 '17 at 17:14

0 Answers0