Kevin Vugts Kevin Vugts - 2 months ago 30
iOS Question

AVAudioPlayer gives nill for the path Swift 3 xcode 8.0

I got a problem with my audio player. When I launch my viewcontroller it gives nill on the url's and I have no idea how this is possible.. I tried to debug and look for the problem but I have no clue whats going on.

Here is the code:

var sfxFive: AVAudioPlayer!
var sfxFour: AVAudioPlayer!
var sfxThree: AVAudioPlayer!
var sfxtwo: AVAudioPlayer!
var sfxOne: AVAudioPlayer!
var sfxGo: AVAudioPlayer!

override func viewDidLoad() {

do {

let resourcePathFive = Bundle.main.path(forResource: "five", ofType: "m4a")!
let urlFive = URL(fileURLWithPath: resourcePathFive)
try sfxFive = AVAudioPlayer(contentsOf: urlFive)

let resourcePathFour = Bundle.main.path(forResource: "four", ofType: "m4a")!
let urlFour = URL(fileURLWithPath: resourcePathFour)
try sfxFour = AVAudioPlayer(contentsOf: urlFour)

let resourcePathThree = Bundle.main.path(forResource: "three", ofType: "m4a")!
let urlThree = URL(fileURLWithPath: resourcePathThree)
try sfxThree = AVAudioPlayer(contentsOf: urlThree)

let resourcePathTwo = Bundle.main.path(forResource: "two", ofType: "m4a")!
let urlTwo = URL(fileURLWithPath: resourcePathTwo)
try sfxtwo = AVAudioPlayer(contentsOf: urlTwo)

let resourcePathOne = Bundle.main.path(forResource: "one", ofType: "m4a")!
let urlOne = URL(fileURLWithPath: resourcePathOne)
try sfxOne = AVAudioPlayer(contentsOf: urlOne)

let resourcePathGo = Bundle.main.path(forResource: "go", ofType: "m4a")!
let urlGo = URL(fileURLWithPath: resourcePathGo)
try sfxGo = AVAudioPlayer(contentsOf: urlGo )


} catch let err as NSError {

func timerRunning() {

if timeElapsed == 5 {
}else if timeElapsed == 4 {
}else if timeElapsed == 3 {
}else if timeElapsed == 2 {
}else if timeElapsed == 1 {
}else if timeElapsed == 0 {

The audio files are located in the root directory.

Does anybody see the problem?

Kind Regards,


Check yourProject -> Target's Build, In that under Copy Bundle Resources phase group,, if your m4a file is not listed, then Add them in that group.

Clean your project and Run again.