Playing Sounds/Audio – Audio Session ServicesTweet
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:
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…