SHARE:
Uncategorized

Recording and Playing Audio using AVFoundation Framework

Flatiron School / 29 October 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.

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:

#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];
}

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

https://developer.apple.com/library/ios/DOCUMENTATION/AVFoundation/Reference/AVFoundationFramework/_index.html

To Vim or Not to Vim Previous Post A Simple Sinatra Buzzer App Next Post