Can't find URL from files using Bundle main url

Viewed 7364

I'm trying to play some small .wav files using AudioToolBox library, but I just can't get the url for the files that I dragged into the project folder (the folder which contains the classes, etc.)

if let soundUrl = Bundle.main.url(forResource: name, withExtension: "wav") {
    //make use of AudioServicesCreateSystemSoundID...
}
else {
    print("sound wasn't found")
}

I tried all combinations of possible drags to xcode (Copy itens if needed, Create groups, Create folder references) and very frequently I Clean the project, but still I always get "sound wasn't found". I truly don't know what information to give, but my approach was working until some days ago, then it suddenly stopped...

PS: Not sure if it's a useful information but I printed the list of files at Bundle.main.bundlePath and I didn't find any of the files I wanted, although they all appear in the list of files in the left (that contains codes, xcassets, Main.storyboard, etc.)

3 Answers
  • Click at one of your wav files
  • At the right side, there will be a list of options
  • The last option of this list is a tab called Target Membership
  • Check if this wav file you chose marks your current project as target

hope that helps :)

Daniel's answer is right in this case. However this message can also occur because the code is in a framework bundle that is part of the project's workspace. In this case, Bundle.main will get you the main project bundle and you should instead use a different bundle constructor, such as Bundle(identifier: <id>) or Bundle(for: type(of: <object>)) (where object is a class appearing in the bundle you want to load from).

If anyone is getting this error while using react native, make sure that you've added your model to xcode workspace (xcode -> project's left side menu -> add files to "Yout Prj" -> select your file). I had this error when I added my model in vs code and xcode didnt know where to find.

Related