Recording and Playing Audio
The AVAudioRecorder class is part
of the AVFoundation framework and provides audio recording capabilities
for an application. The framework allows you to:
Record until the user stops the recording
Record for a specified duration
Pause and resume a recording
The corresponding AVAudioPlayer
class (also part of the AVFoundation framework) provides some fairly
sophisticated functionality allowing you to play sound in your
application. It can:
Play sounds of any duration
Play sounds from files or memory buffers
Loop sounds
Play multiple sounds simultaneously (one sound per audio player) with precise synchronization
Control relative playback level and stereo positioning for each sound you are playing
Seek to a particular point in a sound file, which supports such application features as fast forward and rewind
Recording Audio
Lets build a simple application to record some audio to a file and play it back later. Open Xcode and start a new View-based Application, naming it Recorder when requested.
When the Xcode project opens, add both the AVFoundation and CoreAudio frameworks to the project in a similar manner as we added the MediaPlayer framework to the Audio application earlier in the chapter.
Click on the RecorderViewController.h interface file to open it in the Standard Editor and make the following changes to the template file generated for you by Xcode:
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface RecorderViewController : UIViewController ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access