How to Record and Play Audio With AVFoundation Framework in IOS?

Hey everyone as we have already created an application using AvFoundation library. Similar to this, we will be using the same library to create our own custom through which we can play and record an audio file. In this tutorial, I am going to walk you through the basics of this framework and show you how to manage audio playing, as well as, recording.

To provide you with an example, I will build a simple audio application that allows users to record and play audio. In this tutorial, my primary focus is to demonstrate the AV Foundation framework so the user interface of our application is very simple.

The AVFoundation provides easy ways to deal with audio. In this tutorial, we will be going to deal with these two classes:

  • AVAudioPlayer – An audio player for playing audio files. By using the player, the user can play sounds of any duration and in any audio format like- mp3 etc available in iOS.
  • AVAudioRecorder – An audio recorder for recording audio within the application.

Initial Setup

First of all, we will be going to run the XCode and then we need to click on the create new application button. Later on, we need to select the single view application as shown below.

Now after this step, we will be going to give a name to our application. For my application, I will name it as “AudioRecordingSample”.

Adding AVFoundation Framework

By default, the AVFoundation framework is not included in any Xcode project. So, we need to add it manually. In the Project Navigator of the application, select the “AudioRecordingSample” project.

In the Content Area, select “AudioRecordingSample” under Targets and click “Build Phases”. Expand “Link Binary with Libraries” and click on the “+” button to add the “AVFoundation.framework” to your project.

Here is the snapshot to which I am adding AVFoundation Framework.

To use the AVAudioPlayer and AVAudioRecorder class and its features, we need to import it in ViewController.h.

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioRecorderDelegate, AVAudioPlayerDelegate>


Interface Designing

Here, we will be going to design a simple yet attractive interface for our application. This will consist of three things here:

  1. A button through which we will be going to play the audio from the application through AV Library and will name it as the Record button. Now set the constraints to it as Top layout, center horizontal, equal width and equal height.
  2. A second button just below the first button through which we will be going to stop the recording after we finish. Here set the constraints to Top to button 1 and center horizontally, equal width and equal height to button 1.
  3. The third button below the second stop button, through which we will be going to record the audio in the application. In this area, set the constraints as top to button second, center horizontally and equal width, equal height with the second button.

After performing all the steps in designing your view will looks similar to this as shown below.

Connections With Outlets

Now we need to create a connection between the outlets and objects with the view controller so that we can use them to perform some functionality later in our app. First, we need to connect all the three buttons simultaneously and then later will going to create their actions in the application as well.

Head to the view controller.h file of the application and add the given line of code to it:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIButton *Play;

- (IBAction)playAction:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UIButton *stop;

- (IBAction)stopAction:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UIButton *record;

- (IBAction)recordAction:(UIButton *)sender;


Initializing Recording using AVAudioRecorder

First, let’s take a look how we can use AVAudioRecorder to record audio. Add the AVAudioRecorderDelegate protocol and AVAudioPlayerDelegate in the ViewController.m file. I will be going to explain both delegates as we move forward in the application development.

#import “ViewController.h”

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioRecorderDelegate, AVAudioPlayerDelegate>


Next, declare the two variables to handle and enable to recording objects which are AVAudioRecorder and AVAudioPlayer in ViewController.m:

Latest posts by Rahul Huria (see all)

Leave a Comment