Recording and Playing Audio using AVFoundation Framework

You’re closer than you think to a career in tech. Our grads have launched rewarding new careers — you can too.

View Our Jobs Report

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.

Adding the Framework

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

Screen Shot 2013-10-24 at 9.08.25 PM

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:

1
2
3
4
5
6
7
8
9
#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”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (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:

1
2
3
4
5
6
7
8
9
10
-(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:

1
2
3
4
5
- (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.

Headshot of Flatiron School

Flatiron School

Blog Post Author

Read More Flatiron School Articles

Since we opened our doors in 2012, thousands of students have joined Flatiron School to launch new careers in tech.

Explore our Courses

Find the perfect course for you across our in-person and online programs designed to power your career change.

Explore Our Courses
Join a Community

Connect with students and staff at meetups, lectures, and demos – on campus and online.

Join the Community
Schedule a Chat

Have a question about our programs? Our admissions team is here to help.

Schedule a Chat