Skip to content
Aasim's Web Corner
Aasim's Web Corner

Ink is better than the best memory.

  • Odysseys & Artistry
    • Saunterer Soul
    • My Poetry
    • My Sketch Work
  • Project Management
    • Agile & Frameworks
    • PMP Certification
  • Digital Diary
    • History
    • Islam
    • Life Around Us
    • My Bookshelf
  • My Tutorials
    • Amazon Kindle
    • Android
    • Aspect Oriented Programming
    • BlackBerry
    • Code Repositories
    • iOS
    • Java
    • JavaScript
    • Linux
    • Random Tips
Connect with me
Aasim's Web Corner

Ink is better than the best memory.

How to record Audio in iOS Application;

How to record Audio in iOS Application

Aasim Naseem, December 9, 2012 | Read Count: 13,480April 9, 2025
Category: My Tutorials > iOS

Hello dev mate;XcodeLogo
Hope you had a good weekend and refreshing Monday;

Today we’ll learn how to record audio in iOS application; Now a days, I’m working on an application where I need to record voice of user for some processing; I tried some framework classes from iOS sdk in a test project and here are my findings;

So lets start;


Priminarly we’ll use following classes and framework

AVFoundation.framwork
Provides set of APIs to record/play audio and video in iOS application;

AVAudioRecorder
Called an audio recorder, provides audio recording capability in your application.

AVAudioPlayer
An instance of the AVAudioPlayer class called an audio player, provides playback of audio data from a file or memory;

1. Project Creation and Setup

1. Open you Xcode and create new project .i.e. File -> New -> New Project or  press Command+Shift+N

2. Create Single View Application and provide suitable name; In my case, its AudioRecordingExample;

3. Select project location etc and save it; Your project should look like this

Screen Shot 2012-12-09 at 3.31.58 PM

4. Add required framework to your project .i.e. AVFoundation; For this select your project in Navigator, then select Target at right sight panel; Go to Build Phases, chose Linke Binary with Libraries; Press + button from botom and chose AVFoundation from list;

Screen Shot 2012-12-09 at 3.37.41 PM

After adding framework, you libraries should look like this;

Screen Shot 2012-12-09 at 3.38.01 PM

2. Interface Design

First open the ViewController.xib file and make your screen like this; I’m sure I don’t need to explain how to design it; (:

Screen Shot 2012-12-09 at 4.14.08 PM

3. Declaring Variable in .h file;

Open you ViewController.h file from Navigator and add the following code there;

Implement the following AVAudioRecorderDelegate and AVAudioPlayerDelegate in your  viewController;


@interface ViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>

Include AVFoundation.h in your import section;


#import <AVFoundation/AVFoundation.h>

Add the following two variables, and four UIButtons in your viewController;

//for recording section
AVAudioRecorder *audioRecorder;
IBOutlet UIButton *recordingButton;
IBOutlet UIButton *stopRecordingButton;
//for playback section
AVAudioPlayer *audioPlayer;
IBOutlet UIButton *playButton;
IBOutlet UIButton *stopPlayingButton;

The name of variables are enough to describe their usage in our program; Bind the outlets with respective buttons on xib file;

Now add following methods in your .h file

-(IBAction) recordAudio;
-(IBAction) stopRecording;
-(IBAction) playAudio;
-(IBAction) stopPlaying;

4. Your .m file, actual code

Add the following code in your viewDidLoad method

- (void)viewDidLoad {
[super viewDidLoad];
audioRecorder.delegate = self;
audioPlayer.delegate = self;
playButton.enabled = NO;
stopPlayingButton.enabled = NO;
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error){
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
}

Now add the action methods, bind with each UIButton over screen;

-(IBAction) recordAudio{
if (!audioRecorder.recording){
playButton.enabled = NO;
stopRecordingButton.enabled = YES;
[audioRecorder record];
}
}
//-------------------------------------------------------------------------------------------
-(IBAction)stopRecording{
stopRecordingButton.enabled = NO;
playButton.enabled = YES;
recordingButton.enabled = YES;
if (audioRecorder.recording){
[audioRecorder stop];
}
}
//-------------------------------------------------------------------------------------------
-(IBAction) stopPlaying{
if (audioPlayer.playing) {
[audioPlayer stop];
}
}
//-------------------------------------------------------------------------------------------
-(IBAction) playAudio{
if (!audioRecorder.recording){
stopPlayingButton.enabled = YES;
recordingButton.enabled = NO;
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error];
audioPlayer.delegate = self;
if (error)
NSLog(@"Error: %@", [error localizedDescription]);
else
[audioPlayer play];
}
}

You can also implement these delegate methods, though to keep the tutorial simple, I’m not using them wisely here;


-(void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully:(BOOL)flag{
recordingButton.enabled = YES;
stopPlayingButton.enabled = NO;
}
//-------------------------------------------------------------------------------------------
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
NSLog(@"Decode Error occurred");
}
//-------------------------------------------------------------------------------------------
-(void)audioRecorderDidFinishRecording: (AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSLog(@"Audio Player has finished recording");
}
//-------------------------------------------------------------------------------------------
-(void)audioRecorderEncodeErrorDidOccur: (AVAudioRecorder *)recorder error:(NSError *)error{
NSLog(@"Encode Error occurred");
}
//-------------------------------------------------------------------------------------------

And we are good to go now; Just run the code, tap Record in recording section and speak something in your microphone; Hit stop and press Play in playback section; You’ll hear yourself for sure;

The audio file has saved in /Document directory of your project, and you can get its content by following code, for further uses;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:<your_file_name>];
NSData *fileContents = [NSData dataWithContentsOfFile:filePath];

Thats it; You have done infact;

Now its time to look some new concepts; Back to code when we initialized our AVAudioRecorder instance, we provide it a dictionary of setting, as given below

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error];

This dictionary is holding the different parameters for the audio we are going to capture and record; It inclues Bit rate, Sample rate, Number of channels etc; Let discuss them one by one in a concise way;

Bit Rate
Bitrate refers to the number of bits—or the amount of data—that are processed over a certain amount of time; In audio, this usually means kilobits per second; For example, if you have a song with bitrate of 256 kilobits per second, meaning there are 256 kilobits of data stored in every second of a song; Multiply the bitrate with length of your song and you’ll get the file size; The higher the bitrate of a track, the more space it will take up on your computer;

Audio Channel
A channel in audio is just one separate stream of audio information; Mono audio sources have one channel, Stereo sources have two (left and right), or in more simple words Mono is 1 audio channel (You hear the same sound in both ears) where as Stereo is 2 audio channels (Each headphone can hear a different sound)
Now a days, the normally 5.1 audio channel are being use, having five normal audio channels (Left, Center, Right, Left Surround, Right Surround)

Sample Rate
Sample rate is the number of samples of audio carried per second; In simple words it is a frequency of audio; Data equal to bit rate is passed to processor to generate sample rates (audio frequency); More sample rate means more frequency and hence more clear, smooth audio listening experience;
44.1 kHz is the sampling rate of audio CDs giving a 20 kHz maximum frequency; 20 kHz is the highest frequency generally audible by humans, so making 44.1 kHz the logical choice for most audio material.

Read the code again for initializing the AVAudioRecorder instance; Hope this time you’ll have better understanding;

Happy coding; (:

Flag Counter

Author Profile

Aasim Naseem
Hey, Thanks for your interest. I’m a PMP, AWS Solutions Architect, and Scrum Master certified professional with 17+ years of hands-on experience leading projects, building teams, and helping organizations deliver software solutions better, faster, and smarter.

Outside of work, I’ve got a deep curiosity for history — especially ancient civilizations like Egypt. I also enjoy reflecting on the everyday moments that shape how we live and work. This blog is my space to share insights, lessons, and thoughts from both my professional journey and personal interests.

Thanks for reading — and I hope you will find something here that matches your interest.
Latest entries
  • Economic impact of Eid ul Adha - AasimNaseem.comIslamJune 6, 2025 | Read Count: 283Economic impact of Eid-ul-Adha
  • Best PMP Exam Study Resources - AasimNaseem.comPMP CertificationMay 23, 2025 | Read Count: 493Best PMP Study Resources for 2025 (Books, Courses, Tools & More)
  • agile vs scrum - AasimNaseem.comAgile & FrameworksMay 7, 2025 | Read Count: 463Agile vs Scrum: Finally Understanding the Difference
  • When Agile shouldn’t Use - AasimNaseem.comAgile & FrameworksApril 25, 2025 | Read Count: 493When Not To Use Agile: 5 Signs You Need a Different Approach
iOS AVAudioPlayerAVAudioRecorderAVFoundationhow to recored audio in iphoneiPhonevoice recordingxcode

Post navigation

Previous post
Next post

Related Posts

iOS Beta Builder - AasimNaseem.com

Beta Builder :: Wireless Distribution of Your Adhoc Archive

June 5, 2013 | Read Count: 12,472April 9, 2025

Category: My Tutorials > iOS Hey .. how’s work going? It’s good Han !!! … good good … So you have done with your release and verified with QA after some bug fixing; That’s great; You done a good job; Now Its time to share the Adhoc with client; Ok…

Read More
iOS

Not enough frames in Stack – iO|

January 10, 2011 | Read Count: 14,500May 5, 2025

Category: My Tutorials > iOSIf you are getting this error message in your console while running your app on iPhone/iPod (specially on 1G, having ios < 3.2) dyld: Symbol not found: _OBJC_CLASS_$_UIPopoverController Referenced from: /var/mobile/Applications/5BC198AF-22AF-4581-8787-E267D359E059/FilesAnyWhere.app/<your_app_name> Expected in: /System/Library/Frameworks/UIKit.framework/UIKit in /var/mobile/Applications/5BC198AF-22AF-4581-8787-E267D359E059/FilesAnyWhere.app/<your_app_name> <em>Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not…

Read More
iOS How to symbolicate iPhone Crash Reports - AasimNaseem.com

How to symbolicate iPhone Crash Reports;

May 10, 2011 | Read Count: 14,453May 5, 2025

Category: My Tutorials > iOSHello Everyone; Hows your developments going? Im sure you enjoy it as much as i do; Its really a fun; Today’s menu is related to iPhone Applications; We often notice that iPhone/iPod/iPad applications hangup or exit abnormally; It means the running application has been crashed due to some…

Read More

Comments (4)

  1. kaliyarajalu says:
    August 9, 2013 at 8:07 am

    Such a nice code… Thanks for Your great Help… keep post like advanced things with more explanations

    Reply
  2. Sagar says:
    July 26, 2014 at 6:34 am

    Great tute, was just wondering how would we save audios permanently and display it in table view so one can go back to it later and play it? Thanks

    Reply
    1. Ans says:
      February 26, 2015 at 10:22 am

      Can you please elaborate your requirements bit more? How your screen look like, and how do you want to save audios in TableView?

      Reply
  3. John E says:
    February 26, 2015 at 5:28 am

    Good web site you’ve got here.. It’s difficult to find high quality writing like yours these
    days. I honestly appreciate individuals like you!
    Take care!!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Economic impact of Eid-ul-Adha
    Economic impact of Eid-ul-Adha
    June 6, 2025 | Read Count: 283
  • Best PMP Study Resources for 2025 (Books, Courses, Tools & More)
    Best PMP Study Resources for 2025 (Books, Courses, Tools & More)
    May 23, 2025 | Read Count: 493
  • Agile vs Scrum: Finally Understanding the Difference
    Agile vs Scrum: Finally Understanding the Difference
    May 7, 2025 | Read Count: 463
  • When Not To Use Agile: 5 Signs You Need a Different Approach
    When Not To Use Agile: 5 Signs You Need a Different Approach
    April 25, 2025 | Read Count: 493
  • Quran on Peace and Kindness
    Quran on Peace and Kindness
    April 20, 2025 | Read Count: 452

Posts from Past

  • June 2025
  • May 2025
  • April 2025
  • January 2025
  • November 2024
  • April 2024
  • October 2022
  • August 2021
  • September 2020
  • May 2020
  • April 2019
  • January 2019
  • September 2018
  • July 2015
  • June 2015
  • November 2014
  • September 2014
  • April 2014
  • June 2013
  • May 2013
  • February 2013
  • January 2013
  • December 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009

Categories List

  • Agile & Frameworks
  • Amazon Kindle
  • Android
  • Aspect Oriented Programming
  • BlackBerry
  • Blog
  • Code Repositories
  • History
  • iOS
  • Islam
  • Java
  • JavaScript
  • Life Around Us
  • Linux
  • My Bookshelf
  • My Poetry
  • My Sketch Work
  • PMP Certification
  • Random Tips
  • Saunterer Soul

Recent Posts

  • Economic impact of Eid-ul-Adha
  • Best PMP Study Resources for 2025 (Books, Courses, Tools & More)
  • Agile vs Scrum: Finally Understanding the Difference
  • When Not To Use Agile: 5 Signs You Need a Different Approach
  • Quran on Peace and Kindness

Recent Comments

  1. Aasim Naseem on When Not To Use Agile: 5 Signs You Need a Different Approach
  2. Aasim Naseem on When Not To Use Agile: 5 Signs You Need a Different Approach
  3. Masjid Wazir Khan, Lahore Pakistan - Aasim's Web Corner on Everlasting Art of Badshahi Masjid Lahore Pakistan
  4. Rishi Kumar on When Not To Use Agile: 5 Signs You Need a Different Approach
  5. Best PMP Study Resources for 2025 (Books, Courses, Tools & More) - Aasim's Web Corner on PMP Exam Eligibility 2025: 3 Things You Need to Know
©2025 Aasim's Web Corner | WordPress Theme by SuperbThemes