0

I'm trying to store my sounds in a separate folder called sounds but as I've limited experience with Swift I'm not sure how to concatenate the directory location, so instead of just storing my sounds in the root directory of my project, I'd like it to have a separate folder.

My current code is:

func setupAudioPlayerWithFile(file: NSString, type: NSString) -> AVAudioPlayer? {
    let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
    let url = NSURL.fileURLWithPath(path!)
    var audioPlayer: AVAudioPlayer?

    if path != nil {
        do {
            try audioPlayer = AVAudioPlayer(contentsOfURL: url)
        } catch {
            print("Player not available")
        }
    }

    return audioPlayer
}

So, instead of just dropping my sounds in the root directory, how do I code it so I can have 'Sounds/sound_one.mp3'

Thanks

  • Basically it's not allowed to save files inside the application bundle. You have to save it in the folders `Documents` or `Application Support` of the app's container. – vadian Jan 31 '16 at 16:10
  • If you put the sound file anywhere in your project, you won't have any control on where it is placed after the app is compiled. What you could do is put the assets in a bundle and load that bundle instead of mainBundle. Then proceed to use pathForResource on the new bundle. – John Bennedict Lorenzo Jan 31 '16 at 16:12
  • I tried adding 'Sounds/' + file but that didn't work, surely this is possible? – Jasmin_Long Jan 31 '16 at 16:23

0 Answers0