Back to Blog

Recording and Playing Audio using AVFoundation Framework

Posted by Flatiron School on October 29, 2013

The following is a guest post by Basar Akyelli and originally appeared on his blog. Basar is currently in the iOS-000 class at The Flatiron School. You can follow him on Twitter here.

Today at Flatiron School, I implemented a basic Audio Recorder and Player to one of our apps.

Here’s a short tutorial to implement these controls using AVFoundation Framework.

1. Create a Xcode project with a single view.

2. Add “AVFoundation Framework” to your new project.

Blog post image: screen-shot-2013-10-24-at-8-59-12-pm.png

3. Let’s add two buttons to our app and create their respective IBActions and IBOutlets to our ViewController.

Blog post image: screen-shot-2013-10-24-at-9-08-25-pm.png

4. Now, in our implementation file, after importing the “AVFoundation” framework, let’s define two instance variables. One for our player, and one for the recorder:

#import “ViewController.h”
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()
{
AVAudioPlayer *myPlayer;
AVAudioRecorder *myRecorder;
}
@end

5. We will now need to prepare the AVAudioSession for recording. We can do this in a new method, called “prepareForRecording”

- (void)viewDidLoad
{
[super viewDidLoad];
[self prepareForRecording];
}

-(void)prepareForRecording
{
NSArray *recordingPathComponents =
[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],@“My-Recording.m4a”,nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:recordingPathComponents];

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

NSMutableDictionary *myRecorderSettings = [[NSMutableDictionary alloc] init];

[myRecorderSettings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[myRecorderSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[myRecorderSettings setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

myRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:myRecorderSettings error:NULL];
myRecorder.meteringEnabled = YES;
[myRecorder prepareToRecord];
}

6. Here, we create a path to a audio file in our documents directory. Then we prepare the AVAudioSession object that iOS provides to us. Finally we set up our recorder with our preferred settings. We are now ready to do some recording! Let’s implement our Record button action:

-(void)recordPressed:(id)sender
{
if (!myRecorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[myRecorder record];
} else {
[myRecorder stop];
}
}

7. We’re basically checking to see if we’re already recording. If not, we start recording. If we’re already in the middle of recording, we stop the recording process. So we can use the same button to record/stop depending on our state. Finally, let’s play back our recording:

- (IBAction)playPressed:(id)sender {
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:myRecorder.url error:nil];

[myPlayer play];
}

That’s it! For more information about the AVFoundation framework, please visit Apple’s documentation.

About Flatiron School

More articles by Flatiron School