4

我正在开发一个 iPhone 应用程序。在那里,需要暂停和恢复相机。所以我用AVFoundation它而不是使用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;
        }
    }

当我将相机更改为前置时,我遇到了这个问题。当我通过将相机更改为前置来调用上述方法时,预览层被卡住并且没有预览。我的疑问是“我们可以在捕获会话中间更改捕获设备吗?”。请指导我哪里出错(或)建议我提供有关如何在录制时在前后摄像头之间导航的解决方案。

提前致谢。

4

3 回答 3

5

是的你可以。您需要满足的只是一些事情。

  1. 需要使用 AVCaptureVideoDataOutput 及其委托进行录制。
  2. 确保在添加新的 deviceInput 之前删除之前的 deviceInput。
  3. 您还必须删除并重新创建 AVCaptureVideoDataOutput。

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

- (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];
}
于 2014-03-27T21:37:16.890 回答
1

您不能在会话中更改 captureDevice。而且您一次只能运行一个捕获会话。您可以结束当前会话并创建一个新会话。会有一点延迟(可能是一两秒,具体取决于您的 CPU 负载)。

我希望 Apple 允许多个会话或每个会话至少多个设备……但他们还没有……。

于 2013-08-27T18:38:02.007 回答
1

您是否考虑过进行多个会话,然后处理视频文件以将它们合并为一个?

于 2013-08-27T19:05:21.500 回答