Avfoundation - 同时播放和录制视频(以及音频和预览) [英] Avfoundation - Play and record video (along with audio and preview) simultaneously

查看:251
本文介绍了Avfoundation - 同时播放和录制视频(以及音频和预览)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试同时录制和播放视频。这有可能与avfoundation?目前我能够做到这一点,只要我不录制音频。只要我将音频输入添加到AVCaptureSession并重新启动整个内容,我就会收到AVCaptureSessionWasInterruptedNotification并停止录制。

I am trying to record and play video simultaneously. Is this possible with avfoundation? Currently i am able to do it as long as i dont record audio. As soon as i add audio input to AVCaptureSession and restart the whole thing i receive "AVCaptureSessionWasInterruptedNotification" and recording stops.

这就是我播放视频的方式。

This is how i play video.

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]     initWithContentURL:[NSURL fileURLWithPath:path]];
[moviePlayer.view setFrame:self.playerView.bounds];
moviePlayer.useApplicationAudioSession=NO;
self.player = moviePlayer;

[moviePlayer release];

[self.playerView addSubview:player.view];

[player play];

这就是我录制视频的方式:

And this is how I record video:

NSError *error;

AVCamCaptureManager *captureManager = [[AVCamCaptureManager alloc] init];



if ([captureManager setupSessionWithPreset:AVCaptureSessionPresetLow error:&error])
{
    [self setCaptureManager:captureManager];



     AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[captureManager session]];
     self.captureVideoPreviewLayer= previewLayer;

     UIView *view = [self cameraView];
     CALayer *viewLayer = [view layer];
     [viewLayer setMasksToBounds:YES];

     CGRect bounds = [view bounds];


     [captureVideoPreviewLayer setFrame:bounds];

     if ([captureVideoPreviewLayer isOrientationSupported]) 
     [captureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];


     [captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];


   [[captureManager session] startRunning];

    [self setCaptureVideoPreviewLayer:captureVideoPreviewLayer];

    if ([[captureManager session] isRunning])
    {
        [captureManager setOrientation:AVCaptureVideoOrientationPortrait];
        [captureManager setDelegate:self];


        [viewLayer insertSublayer:captureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

        NSString *countString = [[NSString alloc] initWithFormat:@"%d", [[AVCaptureDevice devices] count]];
        NSLog(@"Device count: %@",countString);


    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                            message:@"Failed to start session."
                                                           delegate:nil
                                                  cancelButtonTitle:@"Okay"
                                                  otherButtonTitles:nil];
        [alertView show];
        [alertView release];

    }
} else {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Input Device Init Failed"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Okay"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];        
}

[captureManager release];
if (![[self captureManager] isRecording]) {
    [[self captureManager] startRecording];
} 

我正在使用来自apple AVCam示例代码的AVCamCaptureManager。

Where as i am using "AVCamCaptureManager" from apple AVCam sample code.

推荐答案

首先,将moviePlayer设置为使用应用程序音频会话:

First, set the moviePlayer to use the application audio session:

moviePlayer.useApplicationAudioSession=YES;

然后,之前调用 [[captureManager session] startRunning] ,激活其类别设置为播放和录制的音频会话,并覆盖其属性以允许其与其他人混合。

Then, before calling [[captureManager session] startRunning], activate an audio session with its category set to "play and record" and override its property to allow it to mix with others.

// Set audio session category to "play and record"
NSError* error = nil;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]) {
    NSLog(@"AVAudioSession setCategory failed: %@", [error localizedDescription]);
}

// Set audio session property "allow mixing" to true so audio can be recorded while it is playing
UInt32 allowMixing = true;
OSStatus status = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
if (status != kAudioSessionNoError) {
    NSLog(@"AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers) failed: %ld", status);
}

// Activate the audio session
error = nil;
if (![audioSession setActive:YES error:&error]) {
    NSLog(@"AVAudioSession setActive:YES failed: %@", [error localizedDescription]);
}

这篇关于Avfoundation - 同时播放和录制视频(以及音频和预览)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆