更改输入类型时AVCaptureSession停止

时间:2017-07-18 12:53:55

标签: ios swift swift3 avcapturesession

在我的Swift项目中,我有一个相机功能。相机用于视频录制,具有基本的开始/停止 - 录制按钮和视频预览。一切都有效,除了一件事。我有UIbutton,它将在手机的前后摄像头之间切换。 AVCaptureSession未运行时,目标函数按预期工作。但启动后,按钮将停止AVCaptureSession而不是仅更换相机。

这是我的功能(由开关按钮触发的功能):

@objc func switchCameraClicked() {
    let session: AVCaptureSession = captureSession
    session.beginConfiguration()


    if (captureDevice?.position == .front) {
        captureDevice = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
    } else {
        captureDevice = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .front)
    }

    if let inputs = session.inputs as? [AVCaptureDeviceInput] {
        for input in inputs {
            if input.device.deviceType != AVCaptureDevice.DeviceType.builtInMicrophone {
                session.removeInput(input)
            } else {
                print("Keep the microphone!")
            }
        }
    }


    do {
        try session.addInput(AVCaptureDeviceInput(device: captureDevice!))
    } catch {
        print(error)
    }

    session.commitConfiguration()
    captureSession = session
}

修改 AVCaptureSession停在session.removeInput(input)并触发func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?)委托方法。

让我听听你的建议。

谢谢!

0 个答案:

没有答案