Skip to Content

Free TUAW iPhone app -- try it now!
AOL Tech

Filed under: How-tos, iPhone

iPhone Coding: Recording Audio

Gallery: VRecord

Yesterday, I figured out how to record audio on the iPhone. Today, by popular request, I'll go through the how-to part. And, for those of you paying close attention, I've updated the application with lots of nice new features. For those of you playing along at home, here are the basics you'll need to know to get started with audio recording.

Frameworks. Surprisingly the whole audio recording thing does not derive from CoreAudio. You'll find the AVRecorder class in the Celestial framework. So in order to use this code, you're going to need to class-dump Celestial.framework. You'll find it on your phone in /System/Library/Frameworks. Copy the framework to your Mac and perform the dump there. Use the class dump as an include file and trim away anything that keeps it from compiling. You'll need to keep AVRecorderPrivate and AVRecorder at a minimum. Make sure you've got some sort of NSObject.h included somewhere in your includes.

#import <celestial.h>

Add an AVRecorder as an instance variable. Since you'll want to start and stop your recording at separate times, make sure whatever class you build can set a new AVRecorder and refer to it later. After you start recording, you'll need to return to that instance and deactivate the recorder.

@interface MyAudioApp : UIApplication {
AVRecorder *recorder; }
@end

Reaquaint yourself with CFURLRef. I know. CFURLRefs are supposed to be pretty interchangable with their NS cousins. But they're not. So you need to use CFURLRef to point to the file you want to record to. For example:

 NSMutableString *fullpathname = @"/var/root/foo.amr";
CFURLRef url;
CFStringRef sref;

sref = CFStringCreateWithCString(nil,
[fullpathname cStringUsingEncoding:
[NSString defaultCStringEncoding]],
kCFStringEncodingASCII);

url = CFURLCreateWithFileSystemPath(nil, sref,
kCFURLPOSIXPathStyle, 0);

Start recording by initializing. Once you get this far, it's just a matter of bookkeeping to get your recorder started. Here's the relevant code. Allocate and initiate the recorder instance, activate it and assign it the CFURLRef URL you've created. Then tell it to start. Recording starts instantly.

 // Start recording

recorder = [[AVRecorder alloc] init];
[recorder activate:self];
[recorder setFilePath:url];
[recorder start];

Stop the recording with "stop". When you're ready to finish recording, just send a stop message to your recorder and deactivate it. The file has already saved automatically to disk. If you want, you can query the number of bytes written before deactivating by sending a recordedFileSizeInBytes message.

 [recorder stop];
[recorder deactivate];

And that's pretty much the whole shebang. It took a lot of work to figure this all out but once figured out it is exceptionally easy to use.

Click here to read all TUAW’s iPhone coverage
jobs & resumes
iPhone developer

Groupon - Chicago, IL (2 weeks ago)

See More Relevant Jobs ›

Related Articles From Our Partners

Get a WordPress.com Blog

Reader Comments (Page 1 of 2)

Tip of the Day

Use Spotlight as a reference tool. Type any word in the Spotlight box and one of the top entries will be a definition. Click on it, and it will bring up the dictionary application to check the word in either the dictionary, thesaurus, Apple database, or Wikipedia.


Follow us on Twitter!
 TUAW [Cafepress]

Featured Galleries

DNC Macs
Macworld 2008 Keynote
Macworld 2008 Build-up
Google Earth for iPhone
Podcaster
Storyist 2.0
AT&T Navigator Road Test
Bento for iPhone 1.0
Scrabble for iPhone
Tom Bihn Checkpoint Flyer Briefcase
Apple Vanity Plates
Apple booth Macworld 07
WorldVoice Radio
Quickoffice for iPhone 1.1.1
Daylite 3.9 Review
DiscPainter
Mariner Calc for iPhone
2009CupertinoBus
Crash Bandicoot Nitro Kart 3D
MLB.com At Bat 2009
Macworld Expo 2007 show floor

 

More Apple Analysis

AOL Radio TUAW on Stitcher