1

iOS 文档说您可以在会话运行时添加和删除输入,例如在前后摄像头之间切换。

但是,当我尝试这个时,我的会话停止了。我锁定会话beginConfigurationcommitConfiguration调用如下:

- (void)switchCamera:(UIButton *)sender {

    dispatch_async([self sessionQueue], ^{

        AVCaptureSession *session = self.captureSession;
        [session beginConfiguration];

        AVCaptureInput *currentInput = self.currentCameraIsBack ? self.videoDeviceInputBack : self.videoDeviceInputFront;
        AVCaptureInput *newInput = self.currentCameraIsBack ? self.videoDeviceInputFront : self.videoDeviceInputBack;

        [session removeInput:currentInput];
        [session addInput:newInput];
        self.currentCameraIsBack = !self.currentCameraIsBack;

        [session setSessionPreset:AVCaptureSessionPresetMedium];
        [self setCameraOutputProperties];

        [session commitConfiguration];
    });
}

我正在输出到AVCaptureMovieFileOutput. 我需要做些什么来配置此会话以使其可切换吗?

(请注意,此问题中的 OP正在尝试添加新输入而不删除旧输入,这不是这里的问题)

4

1 回答 1

0

事实证明,要做到这一点,您需要使用 AVAssetWriter 并调用appendSampleBuffer:自己的结果CMSampleBuffer,如下所示:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {

    if (self.recording && self.videoWriterInput.isReadyForMoreMediaData) {

        [self.videoWriterInput appendSampleBuffer:sampleBuffer];
    }
}
于 2015-05-13T15:32:28.673 回答