录制视频时更改摄像头捕获设备 [英] Change camera capture device while recording a video

查看:109
本文介绍了录制视频时更改摄像头捕获设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发iPhone应用程序。其中,需要暂停和恢复相机。所以我使用 AVFoundation 而不是使用 UIImagePickerController

I am developing an iPhone App. In that, there is a requirement for Pausing and resuming the camera. So i used AVFoundation for that instead of using UIImagePickerController.

我的代码是:

 - (void) startup :(BOOL)isFrontCamera
    {

        if (_session == nil)
        {
            NSLog(@"Starting up server");

            self.isCapturing = NO;
            self.isPaused = NO;
            _currentFile = 0;
            _discont = NO;

            // create capture device with video input
            _session = [[AVCaptureSession alloc] init];
            AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        if(isFrontCamera)
        {
            NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
            AVCaptureDevice *captureDevice = nil;
            for (AVCaptureDevice *device in videoDevices)
            {
                if (device.position == AVCaptureDevicePositionFront)
                {
                    captureDevice = device;
                    break;  
                }  
            }

            cameraDevice = captureDevice;

        }


            cameraDevice=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:nil];

            [_session addInput:input];

            // audio input from default mic
            AVCaptureDevice* mic = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
            AVCaptureDeviceInput* micinput = [AVCaptureDeviceInput deviceInputWithDevice:mic error:nil];
            [_session addInput:micinput];

            // create an output for YUV output with self as delegate
            _captureQueue = dispatch_queue_create("uk.co.gdcl.cameraengine.capture", DISPATCH_QUEUE_SERIAL);
            AVCaptureVideoDataOutput* videoout = [[AVCaptureVideoDataOutput alloc] init];
            [videoout setSampleBufferDelegate:self queue:_captureQueue];
            NSDictionary* setcapSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange], kCVPixelBufferPixelFormatTypeKey,
                                            nil];
            videoout.videoSettings = setcapSettings;
            [_session addOutput:videoout];
            _videoConnection = [videoout connectionWithMediaType:AVMediaTypeVideo];

            [_videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];

            NSDictionary* actual = videoout.videoSettings;
            _cy = [[actual objectForKey:@"Width"] integerValue];
            _cx = [[actual objectForKey:@"Height"] integerValue];
     AVCaptureAudioDataOutput* audioout = [[AVCaptureAudioDataOutput alloc] init];
            [audioout setSampleBufferDelegate:self queue:_captureQueue];
            [_session addOutput:audioout];
            _audioConnection = [audioout connectionWithMediaType:AVMediaTypeAudio];
     [_session startRunning];

            _preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
            _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
        }
    }

我在更换相机时遇到问题前面。当我通过将相机更改为前面来调用上述方法时,预览图层会卡住并且不会进行预览。我怀疑是我们可以在捕获会话中更改捕获设备吗?。请指导我出错的地方(或)建议我在录制时如何在前后摄像头之间导航。

Here i am facing the problem when i change the camera to Front. when i calling the above method by changing the camera to front, the preview layer is getting stuck and no preview is coming. My doubt is "Can we change the capture device in the middle of capture session ?". Please guide me where i am going wrong (or) Suggest me with a solution on how to navigate between front and back camera while recording.

先谢谢。

推荐答案

是的,你可以。您需要满足一些需要的东西。

Yes, you can. There are just a few of things you need to cater to.


  1. 需要使用AVCaptureVideoDataOutput及其代表进行录制。

  2. 确保你在添加新的deviceInput之前删除先前的deviceInput。

  3. 您还必须删除并重新创建AVCaptureVideoDataOutput。

我现在正在使用这两个函数,它在会话运行时有效。

I am using these two functions for it right now and it works while the session is running.

- (void)configureVideoWithDevice:(AVCaptureDevice *)camera {

    [_session beginConfiguration];
    [_session removeInput:_videoInputDevice];
    _videoInputDevice = nil;

    _videoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:camera error:nil];
    if ([_session canAddInput:_videoInputDevice]) {
        [_session addInput:_videoInputDevice];
    }

    [_session removeOutput:_videoDataOutput];
    _videoDataOutput = nil;

    _videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
    [_videoDataOutput setSampleBufferDelegate:self queue:_outputQueueVideo];
    NSDictionary* setcapSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange], kCVPixelBufferPixelFormatTypeKey, nil];

    _videoDataOutput.videoSettings = setcapSettings;
    [_session addOutput:_videoDataOutput];
    _videoConnection = [_videoDataOutput connectionWithMediaType:AVMediaTypeVideo];

    if([_videoConnection isVideoOrientationSupported]) {
        [_videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
    }  

    [_session commitConfiguration];
}

- (void)configureAudioWithDevice:(AVCaptureDevice *)microphone {
    [_session beginConfiguration];
    _audioInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:microphone error:nil];
    if ([_session canAddInput:_audioInputDevice]) {
        [_session addInput:_audioInputDevice];
    }

    [_session removeOutput:_audioDataOutput];
    _audioDataOutput = nil;

    _audioDataOutput = [[AVCaptureAudioDataOutput alloc] init];
    [_audioDataOutput setSampleBufferDelegate:self queue:_outputQueueAudio];
    [_session addOutput:_audioDataOutput];
    _audioConnection = [_audioDataOutput connectionWithMediaType:AVMediaTypeAudio];

    [_session commitConfiguration];
}

这篇关于录制视频时更改摄像头捕获设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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