【问题标题】:Avfoundation - Play and record video (along with audio and preview) simultaneouslyAvfoundation - 同时播放和录制视频(以及音频和预览)
【发布时间】:2011-10-22 22:00:13
【问题描述】:

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

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

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

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

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

我在哪里使用苹果 AVCam 示例代码中的“AVCamCaptureManager”。

【问题讨论】:

    标签: iphone video avfoundation video-capture recording


    【解决方案1】:

    如果其他人想知道...我得到了 Wertesse 提议的上述方法,但它也适用于 AVPlayer(它没有 useApplicationAudioSession 属性)。

    【讨论】:

    • 嗨,Bernt,关于如何让它发挥作用还有什么其他见解吗?我在 viewDidLoad 函数中有效地复制了原始问题中的代码,并添加了 AudioSession 配置。但是,我的 CaptureSession 仍然被停止。代码示例将不胜感激。谢谢!
    【解决方案2】:

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

    moviePlayer.useApplicationAudioSession=YES;
    

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

    // 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]);
    }
    

    【讨论】:

    • 这很有趣,因为它与此处接受的答案相矛盾:stackoverflow.com/a/10559420/954643 表示在捕获会话之后开始音频会话。
    • 实际上,文档对useApplicationAudioSession 是这样说的:“一个布尔值,指示电影播放器​​是否应该使用应用程序的音频会话。(在 iOS 6.0 中已弃用。此属性无法替换,并且不鼓励使用它。)“默认值已经是YES
    猜你喜欢
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多