2

I have to start recorder first which will record the audio during that i need to record video from camera as well.

Code for audio Recording

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:AVAudioSessionCategoryPlayback 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];
    [recorder record];

code for video recording

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [picker dismissViewControllerAnimated:YES completion:^
     {


         if(_isVideo)
         {
             NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
             _imageData = [NSData dataWithContentsOfURL:videoURL];
              [picker dismissViewControllerAnimated:YES completion:nil];
             [ self saveVideo];


         }
     }];




}



-(void)saveVideo
{




        if(_isVideo)
        {

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSDateFormatter *formatter;
            NSString        *dateString;
            formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
            dateString = [formatter stringFromDate:[NSDate date]];
            NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@/%@.mp4",_folderName,@"Videos",dateString]];
            NSError * error = nil;
            NSString *pathVideoThumb = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@/%@",_folderName,@"VideoThumb",dateString]];
            [_imageData writeToFile:path options:NSDataWritingAtomic error:&error];
            if (error != nil)
            {
                NSLog(@"Error: %@", error);
                return;
            }
                [self generateThumbImage:pathVideoThumb dataPath:path];
            dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];

                _isVideo=NO;
                videoCount++;
                [videoLabel setText:[NSString stringWithFormat:@"%d %@",videoCount,@"VIDEOS"]];
                [videoView setBackgroundColor:[UIColor clearColor]];

              });
            });




  }


}

But when i play the recorded Video it does not contain any sound. The recorded audio is working fine ,I understand that the problem is with the audio session ,Please guide me how should i use audio session to handle this.

Thanks

Alok SInha
  • 1,775
  • 1
  • 18
  • 44

0 Answers0