Monday, 28 September 2015

How To Use Audio Play Stop And Pause in Ios/Iphone

==> So  First of All Import Framwork AVFoundation

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

==> And ConfirmDelegete File Method OfAVAudioRecoreder:

@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;

=> Here Declared Diffrent Action Method UIAction:-

- (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:-

    NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               @"MyAudioMemo.m4a",
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

    /=>  Setup Audio Session

    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:-

    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];
}

==> Here 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];
}

- (IBAction)stopTapped:(id)sender {
    [recorder stop];
   
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive:NO error:nil];
}

- (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];
    }
   
    //==> Here Use And 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 Used Osme AvRecordd Delegete File Method Action:-AVAudioRecorderDelegate

#pragma mark - AVAudioRecorderDelegate

- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
    [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
    [stopButton setEnabled:NO];
    [playButton setEnabled:YES];   
}


#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