iphone - Avfoundation - 同时播放和录制视频(以及音频和预览)

标签 iphone video avfoundation video-capture recording

我正在尝试同时录制和播放视频。这有可能吗?目前,只要我不录制音频,我就可以做到。一旦我将音频输入添加到 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”。

最佳答案

首先,将 moviePlayer 设置为使用应用程序 Audio Session :

moviePlayer.useApplicationAudioSession=YES;

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

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

关于iphone - Avfoundation - 同时播放和录制视频(以及音频和预览),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6991452/

相关文章:

php - 使用 YouTube API v3 将视频上传到特定的 PREDETERMINED 帐户

c++ - 如何以编程方式生成碎片 mp4 文件

android - 如何在 webview 上播放视频

ios - AVAudioRecorder - 如何从来电中恢复

iphone - 如何在 html 代码中插入图像?

iPhone如何知道蓝牙耳机是否已连接

iphone - Xcode4:为自定义核心数据管理对象生成不同的代码

ios - UIBarButtonItem 上的自定义背景/透明背景?

ios - iOS从视频中删除特定声音

ios - 相机和照片库权限 - 当用户不接受允许 iOS 10 时处理