Playing Sounds/Audio – Audio Session Services

If you need to play short sounds, less than 30 seconds, Audio Session Services are your friend. Here is a snippet of code to play a wav file:

SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle]
   pathForResource:@"RapidFire" ofType:@"wav"];    
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound (soundID);

For this to work, you will need to import <AudioToolbox/AudioToolbox.h> header file, and also add the AudioToolbox.framework to your project.

For those interested in a little background information, Audio Session Services is a set of functions written in C. There are a few subtleties that give this away. Here is the definition for the AudioServicesCreateSystemSoundID function:

OSStatus AudioServicesCreateSystemSoundID (
   CFURLRef       inFileURL, 
   SystemSoundID  *outSystemSoundID);

The giveaway is how we call this function:

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);

Notice the parameter outSystemSoundID, which is a pointer to SystemSoundID, a 32 bit unsigned integer. To pass this value as a pointer in C, you pass the address of the variable, which is done using &soundID.

More tips on Audio Session Services in the works…

  1. Only problem with this is you cannot control the volume, and the sound will not play through the headphones when the phone is set to vibrate.

  2. Hello,

    I have customized the standard keyboard of UIKeyboardTypeNumberPad type by putting the “.” key in front of the empty light rectangle at the left bottom. But clicking on it the sound does not played. How to get the standard system “click” sound played?


Comments are closed.