8

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.)

1

3 Answers 3

21
  • 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 :)

2
  • I could not find the option above. But I was able to go to "build phases" => targets (the app) => copy bundle resources (I added the resource file right there). It started working after that (the file was read correctly).
    – uudaddy
    Commented Oct 2, 2020 at 15:28
  • @uudaddy can't find that option. I'm going myProject.xcodeproj > Targets: myProject > Build Phases > ??? (I have Dependencies, Compile Sources, Link Binary With Libraries, Copy Files). When I add my file to Copy Files, with Copy only when Installing checked, Path usr/share/man/man1, and Absolute Path) it does not work.
    – blkpingu
    Commented Aug 5, 2022 at 19:28
3

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).

2
  • any good resources on making custom bundles? never had to do this before but I believe this is exactly my problem (first custom framework im making) Commented Jan 4, 2020 at 0:50
  • Sorry, not off the top of my head. I came across the problem because a third-party library included its own bundle of resources.
    – Ash
    Commented Jun 3, 2020 at 7:44
0

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.