My Locations Sound not working

Page 765 - I believe I must have missed something here as I have added the sound resource folder/file needed and I see the “FRIST TIME!” print statement come out on the debug, but no sound. I have tried on simulators as well as a spare iphone 6 I have been using out in the wild to test locations operability.

Attached in is the project file…any help would be appreciated. (552.8 KB)

How did you add the sound resource folder to the project?

I could not see the sound file at all at first but finally found it under the Asset catalog. Any file you add to the asset catalog would generally be treated as an image. So, I don’t believe that Xcode “sees” the file in the same way.

Plus, if you check the code to load the sound effect file, you’ll notice that the code goes through the main bundle to get the URL for the sound file. Putting the sound file inside the Asset Catalog would change the URL for the sound file and so it would not be where we expect it to be as per the code for loading the sound file :slight_smile:

You can check for this quite easily by setting a breakpoint within the loadSoundEffect method and checking to see what happens when the code tries to load the sound file - it will generally just do the first if condition and exit since it can’t find a valid URL for that file.

Of course, it might be a good idea to add an else part to that if condition to notify the user of this situation … Will add that to the next version of the book :slight_smile:

1 Like

Yes, I think I am confused about where to add the sound file. I first added it to the project without success, Then added to the assets without success.

It says Add the Sound folder from the app’s Resources to the project. I figured it belonged in the overall project…I will look at the suggested debugging you provided in your post. Thank you.


1 Like

You can simply add the files to the top-level of the project structure in Xcode :slight_smile: In fact, adding it anywhere in the project, as long as it is not under the Asset Catalog should generally work.

The debugging steps were just to identify what was going wrong. So, simply delete the sounds folder you have added to the Asset Catalog, re-add to the project at the top level (or under the MyLocations folder) and then try running the app again.

ok, so I looked more closely and clearly missing something, even over an adult beverage at this point :slight_smile:

So I added the Sound directory and file, copied option selected into the project file not the assets.

I also did some debugging as you proposed and the sound file unwrapping always fails.

I also looking into the project build phases and the Sound is in there and is at the correct location in the file system.

So again, clearly I am missing something but not sure what…so here is my project and this time you will see Sound quickly :wink:

MyLocations (1.3 MB)

You did everything right … except for one thing :slight_smile: When you added the Sound folder, you probably had the Create folder references radiobox enabled under the Added folders setting. You need to have Create groups enabled instead.

This is a subtle change but it does make a difference here. If you have Create folder references enabled, your folders will appear in blue in Xcode and they actually appear as a sub-folder under the iOS file structure. That is why your code does not work - since the sound file is not in the main bundle but in a sub-folder under the main bundle. So if you remove the sound folder yet again and add it back with the right setting, it should work.

Thank you all works well with that tip. Subtle for sure :wink:

This topic was automatically closed after 166 days. New replies are no longer allowed.