Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Record a sound in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 502
    Comment on it

    To record the sound in ios we can use the code below:-

    To record the sound first set the delegate of AVAudioRecorder in AudioRecorderViewController.h File like

    #import "AudioRecorderViewController.h"

    @interface AudioRecorderViewController : UIViewController<AVAudioRecorderDelegate>

    @interface AudioRecorderViewController : UIViewController<AVAudioRecorderDelegate>


    This will be implemented in  "AudioRecorderViewController.m" file

    AVAudioRecorder *_voicerecorder;
    
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        NSError *err = nil;
        [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
        if(err)
        {
            NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
            return;
        }
        [audioSession setActive:YES error:&err];
        err = nil;
        if(err)
        {
            NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
            return;
        }
        
       NSMutableDictionary  _recorderSettings = [[NSMutableDictionary alloc] init];
        [_recorderSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
        [_recorderSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [_recorderSettings setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
        [_recorderSettings setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        [_recorderSettings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
        [_recorderSettings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
        _audioFileName = @"Sound";
        _recorderFilePath = [NSString stringWithFormat:@"%@/%@.wav", DOCUMENTS_FOLDER, _audioFileName] ;
        NSURL *url = [NSURL fileURLWithPath:_recorderFilePath];
        err = nil;
        _voicerecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:_recorderSettings error:&err];
        if(!_voicerecorder){
            NSLog(@"recorder: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
            [self showAlert:[err localizedDescription]] ;
            return;
        }
        
        [_voicerecorder setDelegate:self];
        [_voicerecorder prepareToRecord];
        _voicerecorder.meteringEnabled = YES;
        BOOL audioHWAvailable = audioSession.inputAvailable;
        if (! audioHWAvailable) {
            NSLog(@"input Hardware not supported");
            
            return;
        }
        
        // start recording
        
        [_voicerecorder record];

     

    if you want to stop the recording then Use the code below

    [_voicerecorder stop];

    Here is the delegate method of the voiceRecorder that notifiy that the recording is successFull or not.
     

    - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
    {
       if(flag){
    
    NSLog(@"Recording finish success'");
    } else{
    
    NSLog(@"Recording finish fail'");
    }  
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: