Skip to Content

iPhone Coding: Recording Audio

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.



Categories

How-tos iPhone

%Gallery-5616% Yesterday, I figured out how to record audio on the iPhone. Today, by popular request, I'll go through the how-to part. And,...
 

Add a Comment

*0 / 3000 Character Maximum

24 Comments

Filter by:
marshall deasy

Bravo! 24 tracks yet??? :)

October 11 2007 at 2:36 PM Report abuse rate up rate down Reply
Langy

Today I have try Recording Audio but my iphone has been crashed when I have clicked on Finish Recording.

Now is blocked in "You are recording..."

I have try to power off the iphone but nothing.

Help me pls.

September 30 2007 at 3:41 PM Report abuse rate up rate down Reply
Erica Sadun

Here's the latest: http://iphone.natetrue.com/VRecord-0.17.tar

August 17 2007 at 5:10 PM Report abuse rate up rate down Reply
BobS

I’ve held my tongue for a week, I’ve tried all the download links but I’ve never been able to locate the “Updated Application” as shown in the “Gallery: VRecord” graphics at the top of this page. The download I get is 150K but when installed on the iPhone I only see the (very basic) single “you are recording” screen as shown in the original VRecord posting. So either the file download host is handing out the wrong download or the graphics at the top are in no way representative of this app! As someone else suggested the download should have a version number in it rather than just VRecord.tar.

Where can I download the correct version??

August 17 2007 at 3:08 PM Report abuse rate up rate down Reply
Jesper

Toll-free bridging may not work on the iPhone, of course. But with both NSString and CFString there, it makes sense that it would.

August 16 2007 at 7:33 PM Report abuse rate up rate down Reply
Jesper

You can cast an NSString (or NSMutableString) to a CFStringRef:

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

sref = (CFStringRef)fullpathname;

It's called "toll-free bridging", it's odd, but it works and it is supported. Google for it for more info.

August 16 2007 at 7:29 PM Report abuse rate up rate down Reply
a

What is the pattern for the number in the filenames?
Could the next version name files by date and time? Pretty please? ^_^

August 11 2007 at 12:39 AM Report abuse rate up rate down Reply
Sean

Can you use an external mic with this?

August 09 2007 at 1:38 PM Report abuse rate up rate down Reply
David

I have an older version of the vrecord and I want to delete it before adding the new version. I have SSH running but when I try and use rmdir on any of the files or VRecord.app directory it tells me permission denied. Any ideas?

August 07 2007 at 10:29 AM Report abuse rate up rate down Reply
vdnyc

so, i'm jailbreaking into the phone, using iphoneinterface to putfile VRecord.app to /Applications, then SSHing to VRecord.app to chmod +x it then restoring my Services.plist and rebooting the phone.

the app still doesn't show up in the springboard. is there a step i'm missing?

i'm excited to check this out. if anyone has any suggestions as to something i may be missing in my workflow, please let me know. thanks!

August 07 2007 at 8:58 AM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.