AVFoundation快速拍摄视频3

问题描述 投票:1回答:1

我一直在学习本教程:https://www.youtube.com/watch?v=w0O3ZGUS3pk,设法到达可以拍照的地方,并可以在UIView上看到相机的输出。

但是我需要录制视频而不是拍照。我已经查看了stackOverflow和AVFoundation帮助中的内容,但是无法正常工作。到目前为止,这是我的代码:

override func viewDidAppear(_ animated: Bool) {
    let devices = AVCaptureDevice.devices()
    for device in devices! {
        if (device as AnyObject).position == AVCaptureDevicePosition.back {
            do {
                let input = try AVCaptureDeviceInput(device: device as! AVCaptureDevice)
                if captureSession.canAddInput(input) {
                    captureSession.addInput(input)
                    if captureSession.canAddOutput(sessionOutput) {
                        captureSession.addOutput(sessionOutput)
                        captureSession.startRunning()
                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                        cameraView.layer.addSublayer(previewLayer)
                        previewLayer.position = CGPoint(x: self.cameraView.frame.width/2, y: self.cameraView.frame.height/2)
                        previewLayer.bounds = cameraView.bounds
                    }
                }
            }
            catch {
                print("error")
            }
        }
    }
}

@IBAction func recordPress(_ sender: Any) {
    if let videoConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo) {

    }
}

我在这里看到了与本教程有关的问题,但没有答案。您将如何使用AVFoundation以这种方式录制视频?

ios swift swift3 avfoundation video-capture
1个回答
0
投票

我用Swift 5 AVFoundation编写了示例项目。一个控制器中的照片和摄像机。在前后摄像头之间具有切换器。通过this link进行检查>

© www.soinside.com 2019 - 2024. All rights reserved.