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
    • 593
    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

    1. #import "AudioRecorderViewController.h"

    @interface AudioRecorderViewController : UIViewController<AVAudioRecorderDelegate>

    1. @interface AudioRecorderViewController : UIViewController<AVAudioRecorderDelegate>


    This will be implemented in  "AudioRecorderViewController.m" file

    1. AVAudioRecorder *_voicerecorder;
    2.  
    3. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    4. NSError *err = nil;
    5. [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
    6. if(err)
    7. {
    8. NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
    9. return;
    10. }
    11. [audioSession setActive:YES error:&err];
    12. err = nil;
    13. if(err)
    14. {
    15. NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
    16. return;
    17. }
    18. NSMutableDictionary _recorderSettings = [[NSMutableDictionary alloc] init];
    19. [_recorderSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    20. [_recorderSettings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    21. [_recorderSettings setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
    22. [_recorderSettings setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    23. [_recorderSettings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    24. [_recorderSettings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
    25. _audioFileName = @"Sound";
    26. _recorderFilePath = [NSString stringWithFormat:@"%@/%@.wav", DOCUMENTS_FOLDER, _audioFileName] ;
    27. NSURL *url = [NSURL fileURLWithPath:_recorderFilePath];
    28. err = nil;
    29. _voicerecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:_recorderSettings error:&err];
    30. if(!_voicerecorder){
    31. NSLog(@"recorder: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
    32. [self showAlert:[err localizedDescription]] ;
    33. return;
    34. }
    35. [_voicerecorder setDelegate:self];
    36. [_voicerecorder prepareToRecord];
    37. _voicerecorder.meteringEnabled = YES;
    38. BOOL audioHWAvailable = audioSession.inputAvailable;
    39. if (! audioHWAvailable) {
    40. NSLog(@"input Hardware not supported");
    41. return;
    42. }
    43. // start recording
    44. [_voicerecorder record];

     

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

    1. [_voicerecorder stop];

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

    1. - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
    2. {
    3. if(flag){
    4.  
    5. NSLog(@"Recording finish success'");
    6. } else{
    7.  
    8. NSLog(@"Recording finish fail'");
    9. }
    10. }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: