user5000215
user5000215

Reputation:

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:

// VARS AUDIO
    var sfxFive: AVAudioPlayer!
    var sfxFour: AVAudioPlayer!
    var sfxThree: AVAudioPlayer!
    var sfxtwo: AVAudioPlayer!
    var sfxOne: AVAudioPlayer!
    var sfxGo: AVAudioPlayer!

override func viewDidLoad() {
        super.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 )

            sfxFive.prepareToPlay()
            sfxFour.prepareToPlay()
            sfxThree.prepareToPlay()
            sfxtwo.prepareToPlay()
            sfxOne.prepareToPlay()
            sfxGo.prepareToPlay()

        } catch let err as NSError {
            print(err.debugDescription)
        }
}


func timerRunning() {

        if timeElapsed == 5 {
            sfxFive.play()
        }else if timeElapsed == 4 {
            sfxFour.play()
        }else if timeElapsed == 3 {
            sfxThree.play()
        }else if timeElapsed == 2 {
            sfxtwo.play()
        }else if timeElapsed == 1 {
            sfxOne.play()
        }else if timeElapsed == 0 {
            sfxGo.play()
        }
}

The audio files are located in the root directory.

Does anybody see the problem?

Kind Regards, Kevin.

Upvotes: 0

Views: 274

Answers (1)

Wolverine
Wolverine

Reputation: 4329

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.

Upvotes: 1

Related Questions