12.3. Recording Audio
Problem
You want to be able to record audio files on an iOS device.
Solution
Use the AVAudioRecorder
class
in the AV Foundation framework.
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 retrieve it from this instance method in case something goes wrong.
The settings
parameter of the
initWithURL:settings:error:
method is
particularly interesting. There are many keys that could be saved in the
settings
dictionary, but we will
discuss only some of the most important ones in this recipe:
AVFormatIDKey
The format of the recorded audio. Some of the values that can be specified for this key are the following:
kAudioFormatLinearPCM
kAudioFormatAppleLossless
AVSampleRateKey
The ...
Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.