==> So First of All Import Framwork AVFoundation:-
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *recordPauseButton;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UISlider *sliderCurrentSongValue;
@property (weak, nonatomic) IBOutlet UILabel *lblDuration;
@property (weak, nonatomic) IBOutlet UILabel *lblTotalDuration;
==> Declared IBAction Of Play Pause and Record:
- (IBAction)recordPauseTapped:(id)sender;
- (IBAction)stopTapped:(id)sender;
- (IBAction)playTapped:(id)sender;
@end
implemnt file some delegate and decalration fiield action method to use:
@interface ViewController () {
AVAudioRecorder *recorder;
AVAudioPlayer *player;
}
@end
@implementation ViewController
@synthesize stopButton, playButton, recordPauseButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Disable Stop/Play button when application launches
[stopButton setEnabled:NO];
[playButton setEnabled:NO];
//==> Set The Audio file Using AvAudioPlayer:
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// ==> Setup Audio Session == AVAudioSwession:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
//==> Define The Recorder Setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
//==> Initiate And Prepare The Recorder:-AVAudioRecorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
==> Updated UI:-
-(void)updateUI{
_lblDuration.text = [NSString stringWithFormat:@"%.2f",player.currentTime];
_sliderCurrentSongValue.value = player.currentTime;
}
- (IBAction)recordPauseTapped:(id)sender {
// Stop the audio player before recording
if (player.playing) {
[player stop];
}
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
[recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
} else {
// Pause recording
[recorder pause];
[recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
}
[stopButton setEnabled:YES];
[playButton setEnabled:NO];
}
==> Stop Recording IBAction:-
- (IBAction)stopTapped:(id)sender {
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
=> Play Recording IBAction Used:-
- (IBAction)playTapped:(id)sender {
//PLay recorded sound
if (!recorder.recording){
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
[player setDelegate:self];
[player play];
_lblTotalDuration.text = [NSString stringWithFormat:@"%f",player.duration];
[_sliderCurrentSongValue setMaximumValue:player.duration];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateUI) userInfo:nil repeats:YES];
}
//Play song from bundle
// [playButton setEnabled:YES];
//
// NSURL *aUrlSong= [[NSBundle mainBundle] URLForResource:@"Bindass PLAY - Choti Choti Khushiyon [192kbps] [Songspkmp3.me]" withExtension:@"mp3"];
// player = [[AVAudioPlayer alloc] initWithContentsOfURL:aUrlSong error:nil];
// [player setDelegate:self];
// [player play];
// _lblTotalDuration.text = [NSString stringWithFormat:@"%.2f",player.duration];
// [_sliderCurrentSongValue setMaximumValue:player.duration];
// [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateUI) userInfo:nil repeats:YES];
}
==> Here Some AVAudioRecorederDeleget Used For AVAudioFoundation:-
#pragma mark - AVAudioRecorderDelegate
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
[recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
[stopButton setEnabled:NO];
[playButton setEnabled:YES];
}
=> Used And Implement AVAudioPlayed Delegate
#pragma mark - AVAudioPlayerDelegate
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Done"
message: @"Finish playing the recording!"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
@end
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *recordPauseButton;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UISlider *sliderCurrentSongValue;
@property (weak, nonatomic) IBOutlet UILabel *lblDuration;
@property (weak, nonatomic) IBOutlet UILabel *lblTotalDuration;
==> Declared IBAction Of Play Pause and Record:
- (IBAction)recordPauseTapped:(id)sender;
- (IBAction)stopTapped:(id)sender;
- (IBAction)playTapped:(id)sender;
@end
implemnt file some delegate and decalration fiield action method to use:
@interface ViewController () {
AVAudioRecorder *recorder;
AVAudioPlayer *player;
}
@end
@implementation ViewController
@synthesize stopButton, playButton, recordPauseButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Disable Stop/Play button when application launches
[stopButton setEnabled:NO];
[playButton setEnabled:NO];
//==> Set The Audio file Using AvAudioPlayer:
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// ==> Setup Audio Session == AVAudioSwession:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
//==> Define The Recorder Setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
//==> Initiate And Prepare The Recorder:-AVAudioRecorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
==> Updated UI:-
-(void)updateUI{
_lblDuration.text = [NSString stringWithFormat:@"%.2f",player.currentTime];
_sliderCurrentSongValue.value = player.currentTime;
}
- (IBAction)recordPauseTapped:(id)sender {
// Stop the audio player before recording
if (player.playing) {
[player stop];
}
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
[recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
} else {
// Pause recording
[recorder pause];
[recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
}
[stopButton setEnabled:YES];
[playButton setEnabled:NO];
}
==> Stop Recording IBAction:-
- (IBAction)stopTapped:(id)sender {
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
=> Play Recording IBAction Used:-
- (IBAction)playTapped:(id)sender {
//PLay recorded sound
if (!recorder.recording){
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
[player setDelegate:self];
[player play];
_lblTotalDuration.text = [NSString stringWithFormat:@"%f",player.duration];
[_sliderCurrentSongValue setMaximumValue:player.duration];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateUI) userInfo:nil repeats:YES];
}
//Play song from bundle
// [playButton setEnabled:YES];
//
// NSURL *aUrlSong= [[NSBundle mainBundle] URLForResource:@"Bindass PLAY - Choti Choti Khushiyon [192kbps] [Songspkmp3.me]" withExtension:@"mp3"];
// player = [[AVAudioPlayer alloc] initWithContentsOfURL:aUrlSong error:nil];
// [player setDelegate:self];
// [player play];
// _lblTotalDuration.text = [NSString stringWithFormat:@"%.2f",player.duration];
// [_sliderCurrentSongValue setMaximumValue:player.duration];
// [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateUI) userInfo:nil repeats:YES];
}
==> Here Some AVAudioRecorederDeleget Used For AVAudioFoundation:-
#pragma mark - AVAudioRecorderDelegate
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
[recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
[stopButton setEnabled:NO];
[playButton setEnabled:YES];
}
=> Used And Implement AVAudioPlayed Delegate
#pragma mark - AVAudioPlayerDelegate
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Done"
message: @"Finish playing the recording!"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
@end