Sunday, June 6, 2010

Quick & Easy iPhone Audio

I spent a lot of time trying to find out how to record audio on the iPhone for 'SoundPad!' only to find that it was actually really easy. So, I'm going to show you guys how to record and play really quickly.

Firstly, make sure you import the 'AVFoundation' framework into your project.
Also, add '#import ' and '#import ' to the top of your file.

In your header file, define an NSMutableDictionary for your record settings.

Now, in the appropriate method, insert the following code:

self.recordSettings = [NSMutableDictionary dictionary];
[recordSettings setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4]
forKey:AVFormatIDKey];
[recordSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSettings setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];

This is not incredibly complex, but too complex for this post. Explanations of this are easily found in Apple's documentation.

Place the following code in a 'record' method:

NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *filePath = [paths objectAtIndex:0] stringByAppendingPathComponent: [NSString
stringWithFormat: @"Recording.caf",]];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithContentsOfURL:fileUrl
error:&error];
[recorder record];

You can also call:

[player setDelegate:delegate];

To allow 'delegate' to respond to certain events (e.g: When the recorder stops recording).

Place the following code in a 'play' method:

NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *filePath = [paths objectAtIndex:0] stringByAppendingPathComponent: [NSString
stringWithFormat: @"Recording.caf",]];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl
error:&error];
[player play];

AVAudioPlayer also responds to 'setDelegate:', and as extra options, you can call:

[player setVolume:num]; //Sets players volume to 'num.' Default value is 1.
[player setNumberOfLoops:num] //Makes player loop 'num' times. Default value is 0. A
//negative value will make player loop for infinity.

Link the 'play' and 'record' methods up to a couple of IBActions and you've got yourself a pretty cool recorder. A lot of this is pretty self explanatory, and the stuff that isn't is easily found in Apple's Docs. There's a little more to this class to do with metering and stuff, but all the core funcionality is shown here.

Well, I hope you liked this quick tutorial and don't forget to come back soon. Bye!

P.S. For those who want to know straight where to go to adjust format of audio and stuff like that, that's what 'recordSettings' is.

P.P.S. I've just seen the preview of this post and I apologise for the poor formatting. If you guys find it IMPOSSIBLE to decode, tell me and I'll upload a simple UIViewController class that uses the above technique.

href="http://digg.com/submit?url=http%3A//dylanelliottiphone.blogspot.com/2010/06/quick-easy-iphone-audio.html">