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.
Share
%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
Bravo! 24 tracks yet??? :)
October 11 2007 at 2:36 PM Report abuse Permalink rate up rate down ReplyToday 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.
Here's the latest: http://iphone.natetrue.com/VRecord-0.17.tar
August 17 2007 at 5:10 PM Report abuse Permalink rate up rate down ReplyIâ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??
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 Permalink rate up rate down ReplyYou 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.
What is the pattern for the number in the filenames?
Could the next version name files by date and time? Pretty please? ^_^
Can you use an external mic with this?
August 09 2007 at 1:38 PM Report abuse Permalink rate up rate down ReplyI 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 Permalink rate up rate down Replyso, 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!
Hot Apps on TUAW
Deals of the Day
more deals- Bracketron Stand with Headrest Mount for iPad 2 for $11 + free shipping
- Philips wOOx Alarm Clock Radio for Apple iPod / iPhone for $60 + free shipping
- iWatchz Elemetal Collection Bracelet for iPod nano for $75 + free shipping
- Skullcandy Ink'd Mic'd Stereo Earbuds for $5 + $2 s&h
- Refurb Logitech Bluetooth Keyboard / Stand for Apple iPad for $31 + $4 s&h
- SanDisk Sansa In-Ear Headphones 2-Pack for free + $4 s&h
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



24 Comments