iPhone Game Development - Chris Craft [126]
afconvert –f caff –d LE16 boom.wav boom.caf
Sound code tends to get mixed and meshed in with all the other things going on in a game, which makes it tough to see the full picture. In the following sections we show you two different ways to fire up sounds in your applications without all the rest of the noise that drowns out the sound code.
Playing simple sounds with AudioToolkit
If you are writing a simple puzzle game or you are just looking to add some sounds to a user interface, you will probably be happy with the services provided by AudioToolkit. The following listing manages to play a sound and pull it off with only four lines of code:
SystemSoundID soundId;
CFURLRef fileURL = (CFURLRef)[[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@”sound” ofType:@”caf”]] retain];
AudioServicesCreateSystemSoundID (fileURL, &soundId);
AudioServicesPlaySystemSound (soundId);
It doesn't get much easier than this. In your actual implementation you'll want to keep an array of sound IDs so that you can load several sounds at once on startup and then play them back in an instant as needed.
Caution
The ease of AudioToolkit does come at a cost: It is rarely powerful enough for intense games.
Making some “real” noise with OpenAL
OpenAL is a whole other beast compared to AudioToolkit. The quality, flexibility, and power of OpenAL is far superior to other options. However, there is a cost for this power: OpenAL has a much steeper learning curve. Here, we are going to show you the basic steps of firing up the OpenAL engine and playing a simple sound. OpenAL is akin to OpenGL in its state machine style of management. Most of your code will be busy setting up state and then you can finally sit back and make short calls to play your sounds.
oalTouch is an example application you can download from the iPhone Dev Center (Figure 9.11). The steps here are as close as possible to the example, except that you are not jumping from method to method to see them. We also took advantage of the class MyOpenALSupport included in the example. This is a big help and it is perfectly acceptable for you to do the same in your applications.
FIGURE 9.11
The main screen from iPhone Dev Center's example oalTouch. For this example, put on your headphones and move the speaker and the listener around. The position and volume of the sound will change with respect to the position of the elements on the screen.
Note
Apple allows anyone to use their examples in their testing and even production applications. In fact, most if not all of the example applications have been compiled and published to the App Store by developers with no changes. (Well, they added their names as the authors.)
Here are the steps to initializing and playing a sound with OpenAL. Your first step is creating an audio session. The following code first initializes the session, then it configures the session, and finally, it activates the session:
// initialize the session
OSStatus status = AudioSessionInitialize(NULL, NULL,
interruptionListener1, self);
if (status)
return false;
// configure the session
UInt32 category = kAudioSessionCategory_AmbientSound;
status = AudioSessionSetProperty(
kAudioSessionProperty_AudioCategory,
sizeof(category), &category);
if (status)
return false;
// activate the session
status = AudioSessionSetActive(true);
if (status)
return false;
After creating an audio session we need to retrieve a reference to the default output device. We can get this by calling alcOpenDevice with no parameters:
ALenum error;
ALCcontext *newContext = NULL;
ALCdevice *newDevice = NULL;
newDevice = alcOpenDevice(NULL);
if (newDevice == NULL)
return false;