Recording audio (iOS 4)

In this section, you’ll learn how to record audio by building a demo app with AVFoun-dation framework.

AVFoundation framework is really easy to use for a simple audio recording or playing task. You can find all the classes for recording audio in the AV Foundation framework. In order to use these classes, you must add AVFoundation. framework to your project. We’ll look at how to initialize and control the audio reader and also how to respond to its associated events.

Initializing the audio recorder

When you’re initializing a new AVAudioRecorder object, you should avoid using the default constructor init. This is to reduce complexity, because this class requires quite a bit of configuration. The constructor you should use is initWithURL:settings: error. It allows you to specify the location on disk to record the audio to as well as provide various audio settings.

The first parameter is the location where the recording will be stored. Although it’s expressed as an NSURL, it’s really a local path that points to a location on disk. In most cases, you’ll want to store recordings in the Documents directory.

The next parameter is an NSDictionary that contains the settings for the recording. Table 12.5 lists some of the settings that you may want to consider when setting up your recorder.

Table 12.5 Basic audio settings for AVAudioRecorder


Setting key

Description

AVSampleRateKey

A sample rate, in Hertz, expressed as an NSNumber floatingpoint value.

AVForma tIDKey

A format identifier. A common value for this is

kAudioFormatAppleLossless.

AVNumberOfChannelsKey

The number of channels expressed as an NSNumber integer value. You can set this value to 1.

AVEncoderAudioQualityKey

A key that refers to the quality of the audio being played.

You can specify quite a few other settings when creating your recorder. All of these settings are optional and have default values; you can use them to fine-tune your audio recording. The next listing demonstrates how to build an AVAudioRecorder object with some basic settings.

Listing 12.3 Initialization code for AVAudioRecorder

Listing 12.3 Initialization code for AVAudioRecorder

 

Listing 12.3 Initialization code for AVAudioRecorder

Note the filePath. This is an NSString that points to a file named recording.caf in the Documents directory. This path is converted to an NSURL during the construction of the recorder.

Controlling the audio recorder

After you construct an AVAudioRecorder, you have quite a bit of control over it. Table 12.6 lists all the methods you can call on a recorder to control the recording session.

The following code shows how to make a simple toggleRecord method that can be used as an IBAction for a button. The code assumes you’ve created a few global properties. Theses properties include recording of type BOOL and soundRecorder of type AVAudioRecorder:

tmp5A88_thumb

Table 12.6 Methods to control audio recording

Method

Description

- (BOOL)prepareToRecord

Creates the recording file on disk at the specified URL path. This method also prepares the system for recording.

- (BOOL)record

Starts or resumes recording. This method implicitly calls the

prepareToRecord method.

- (BOOL)recordForDuration: (NSTimeInterval)duration

Starts the recorder and records for a specified amount of time.

- (void)pause

Pauses a recording. To resume recording, call the record method again.

- (void)stop

Stops the recording and closes the audio file.

- (BOOL)deleteRecording

Deletes the current recording. For this method to work, the recording must be stopped.

When toggleRecord is called for the first time, record is set to NO. This starts the audio recording and sets the recording property to YES. The system creates the recording file and begins receiving input from the built-in microphone. If the device’s headset is plugged in, the system uses the headset’s microphone instead.

The second time toggleRecord is called, the recorder stops recording. This closes the audio file and allows it to be played. The recording property is also set to NO.

Responding to AVAudioRecorder events

Like many API classes, AVAudioRecorder sends messages to a delegate. To respond to delegate actions from the AVAudioRecorder, your class must implement the AVAudio-RecorderDelegate. Table 12.7 describes the methods that can be implemented.

Table 12.7 AVAudioRecorderDelegate methods

Method

Description

tmp5A-89

Called when the recorder finishes recording. This method is passed a reference to the recorder and a Boolean value that’s yes if it was successful.

tmp5A-90

Called when an error occurs during recording.

tmp5A-91

Called when the recording is interrupted. The most common interruption is when the user gets an incoming call while recording.

tmp5A-92

Called when the interruption ends. An example is pressing Ignore in response to an incoming call.

As with most delegate classes, it’s important to implement all of these methods in your class. Doing so ensures that your application responds correctly in any circumstance.

Now that you know how to record audio, the next step is to play it back. The next section will discuss the method for playing your recordings as well as any other audio files in your application.

Next post:

Previous post: