O'Reilly logo

iOS 5 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.3. Recording Audio

Problem

You want to be able to record audio files on an iOS device.

Solution

Make sure you have added the CoreAudio.framework framework to your target file, and use the AVAudioRecorder class in the AV Foundation framework:

NSError   *error = nil;

NSString *pathAsString = [self audioRecordingPath];

NSURL *audioRecordingURL = [NSURL fileURLWithPath:pathAsString];

self.audioRecorder = [[AVAudioRecorder alloc] 
                      initWithURL:audioRecordingURL
                      settings:[self audioRecordingSettings]
                      error:&error];

For information about the audioRecordingSettings and audioRecordingPath methods used in this example, refer to this recipe’s Discussion.

Discussion

The AVAudioRecorder class in the AV Foundation framework facilitates audio recording in iOS applications. To start a recording, you need to pass various pieces of information to the initWithURL:settings:error: instance method of AVAudioRecorder:

The URL of the file where the recording should be saved

This is a local URL. The AV Foundation framework will decide which audio format should be used for the recording based on the file extension provided in this URL, so choose the extension carefully.

The settings that must be used before and while recording

Examples include the sampling rate, channels, and other information that will help the audio recorder start the recording. This is a dictionary object.

The address of an instance of NSError where any initialization errors should be saved to

The error information could be valuable later, and you can ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required