I try to make a Xylophone. I added my Sound file by dragging the file into my XCode project and i made sure 'Copy items if needed' is checked. I wrote the code. I connected 7 buttons to the keyPressed function. I set the names of the sound files and the names of the buttons to be the same. When I run the code and press any key, it gives this error and no sound is playing. I'm sure the sound files are also included because when I click on show in Finder, the audio files are in the project. I don't know what I did wrong.
-
Are you sure the extension of the sound files is `.wav`? – jnpdx Feb 04 '22 at 22:18
-
1yes the extensions are .wav. I checked everything down to the smallest detail but it still doesn't work. – MelisYazıcı Feb 04 '22 at 22:23
-
Are you sure the files are added to your target? – jnpdx Feb 04 '22 at 22:23
-
Each button is bound to the keyPressed function. When i right click on the buttons i see the connection and the sound files are also in the project. When i click on the 'Sounds' file show in Finder it shows files. I don't know what else to do – MelisYazıcı Feb 04 '22 at 22:41
-
Click on the sound file within Xcode and look in the right sidebar -- you need to make sure that it's added to your target. – jnpdx Feb 04 '22 at 22:42
-
Now i checked and finally it works! If you just drag the Sounds file into XCode it wont work, you have to create a new group in XCode then drag the sounds(.wav or .mp3) into this new file otherwise it wouldnt work. Thank you so much you made me realize. – MelisYazıcı Feb 04 '22 at 23:23
-
When your drag items in, it asks you what target to add to — creating a new group isn’t a necessary step, but I’m glad you found a path that works. – jnpdx Feb 04 '22 at 23:43
-
When i drag the file i did it like you said but i guess you have to create a file into XCode then you need to drag the sounds into that file. – MelisYazıcı Feb 04 '22 at 23:54
2 Answers
When you just drag the sounds file into XCode it won't work. You need to create a new group from the file of your code related to the sounds (mine was ViewController in this case.) Then you can drag the sounds into that new file. Thus, you target the audio files.

- 21
- 5
-
This is not true -- creating a new group is not a necessary step. Dragging files into Xcode will let you choose what target to add them to. Even if that target isn't chosen appropriately at the time, the files can be added to the target after the fact. It is not required that they exist in a new/separate group. – jnpdx Feb 05 '22 at 00:01
-
I made a new project just the make sure. I made everything the same and again i try to drag the sounds or the sounds file and i made sure the target situation but it wont work i really dont understand why it worked for the other project but dont work now and everything is the same. https://i.stack.imgur.com/SPWm9.png https://i.stack.imgur.com/84Qsx.png – MelisYazıcı Feb 05 '22 at 00:58
I found the cause of the error. When I change Button's style Plain to Default in Inspector, it works without any problem.
When you wanna add sounds to XCode the safest way is to do, first you should create a new group by right clickin to ViewController. This will create a new file then you can drag your sounds (.wav / .mp3) into that file. While you doing that you need to target them.
After you drag and target them, you should see like this in the inspector when you click each of the sounds:
If you wont change the button style plain to default it wont work whatever you carefully drag the sounds into XCode. I know it's stupid but it should be like this. Otherwise when you run the project and click on any button it would give an error like that:

- 21
- 5