在ios中同时录制音频和视频

时间:2014-08-05 13:30:10

标签: ios ios7 audio-recording video-recording audiosession

我必须首先启动录音机,这将记录音频,因为我需要录制来自摄像机的视频。

音频录制代码

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

视频录制代码

-(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]];

              });
            });




  }


}

但是当我播放录制的视频时,它不包含任何声音。录制的音频工作正常,我明白问题出在音频会话上,请指导我如何使用音频会话来处理这个问题。

由于

0 个答案:

没有答案