Thursday, May 23, 2013

IPhone: Playing Basic Audio

This post is for beginner level programmers and hence 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/continuous music. However, the downfall of doing audio this way is only one audio file can be playing at a time.

Let's get started

Lets assume the two sounds we want to play are:
* drumSound.caf
* pianoSound.caf

Notice that both 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.

Step 1: Adding Audio Files
Drag the required audio files to your Resources Folder inside of your project. Make sure you check the box to copy the files into your project’s directory.

Step 2: Add The AudioToolbox.framework to Your Project
Right Click on the Frameworks folder in your project and select Add -> Existing Frameworks. Search for AudioToolbox.framework and when found click Add.

Step 3: Add the Header Declarations
Open up your ViewController and add the following code into its .h file


SystemSoundID drumSoundID;
SystemSoundID pianoSoundID;

@property(nonatomic) SystemSoundID drumSoundID;
@property(nonatomic) SystemSoundID pianoSoundID;

Now open up the .m file of the same ViewController and add the following code:
@synthesize drumSoundID, pianoSoundID;

Now add the following code in viewDidLoad method to initialize our sounds object.

NSString *drumPath = [[NSBundle mainBundle] pathForResource:@"drumSound" ofType:@"caf"];
CFURLRef drumURL = (CFURLRef) [NSURL fileURLWithPath:drumPath];
AudioServicesCreateSystemSoundID (drumURL, &drumFileID);

NSString *pianoPath = [[NSBundle mainBundle] pathForResource:@"pianoSound" ofType:@"caf"];
CFURLRef pianoURL = (CFURLRef) [NSURL fileURLWithPath:pianoPath];
AudioServicesCreateSystemSoundID (pianoURL, &pianoFileID);

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 &drumFileID. 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.

Step 4: Playing the Sounds
t’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 drum sound, just call this method:

-(void)playDrumSound {
AudioServicesPlaySystemSound (drumFileID);

Same way we could play the other sound anytime depending on our requirement. This tutorial would help you in adding any basic audio to your project.