iPhone Game Programming Tutorial Part 4 – Basic Game Audio

May 4th, 2009 Posted by: - posted under:Tutorials

Ok, I will be finishing up this series by showing you guys how to add basic audio to your game. Since this is a beginner tutorial, I won’t go into complex audio using OpenAL or Audio Queues. Today, we will just be playing short sounds (less than 30 seconds). This will be the easiest way to do audio if you don’t require any background music (I guess you could do a nasty hack and loop a 30 sec clip but that would be shameful :) ). The downfall of doing audio this way is only one audio file can be playing at a time. Ok, let’s get started… Make sure you are starting with the base code from the previous tutorial (either use yours or download a fresh copy). We will be using 2 sounds today, 1 for when the ball is hit and another for when someone scores a point.

Here are the sound files you will need to download for this tutorial:

Notice that all of the audio files have a .caf extension. This is the audio type all of your sound files must be in to be played by the iPhone. Luckily Apple has provided us a utility for converting audio to this format. You can read up on it here. It’s pretty straight forward, just open up a Terminal and type

/usr/bin/afconvert -f caff -d LEI16 {INPUT} {OUTPUT}

Where {INPUT} is the path to your input audio file and {OUTPUT} is the file you want it to save it to with a .caf extension.

Adding the Audio Files To You Project

Start by downloading these files. Then drag them to your Resources Folder inside of your project. You may also want to add a subfolder called Audio to keep organized. Make sure you check the box to copy the files into your project’s directory.

screenshot_01

Add The AudioToolbox.framework to Your Project

Since we need to call functions within this framework, we will need to add it to our project. The best way I have found to locate a framework on my mac is to search for it. Right Click on the Frameworks folder in your project and select Add -> Existing Frameworks. In the search box type AudioToolbox.framework and select the framework among the search results. (Make sure you have Computer selected to search your entire computer)

screenshot_02

When you find it, click Add. We are now ready to start coding…

Add the Header Declarations

Open up iTennisViewController.h and add the following code.

screenshot_11

First we see an import for the AudioServices.h. This is needed to call the functions required for us to play audio. Next, we declare 2 SystemSoundID’s. These are basically just Integers (could actually use an int instead but you’d get a warning). Next, open up iTennishViewController.m and add the following code to synthesize these variables.

screenshot_09

Now let’s initialize our sounds objects. Go to the viewDidLoad method and add the following code.

screenshot_12

Pretty straight forward. The first line gets the path of the audio file. The next line converts the path to a CFURLRef. Finally, we load that audio file and associate it with the given sound ID. Notice we pass &clappingFileID. This is a pass by reference and allows the AudioServicesCreateSystemSoundID method to modify this variable. We do the same thing to load the volley sound. Now, let’s play the sounds.

Playing The Sounds

It’s actually quite simple to play the sounds we have loaded. Simply call the AudioServicesPlaySystemSound method and pass it the soundID of the file you want to play. So to play the clapping sound when someone scores, add this code to your reset method.

screenshot_14

Notice, we are passing the clappingFileID to indicate we want to play the clapping sound. Now, add the following code to gameLoop to play the racquet sound when someone hits the ball.

screenshot_15

Pretty straight forward Right? Now click Build and Go and check out the game audio. That concludes this tutorial. If you have any questions or comments, feel free to leave them in the comments section of this post or write them to me on Twitter. You can also download the sample code here

Happy iCoding!