我一直在学习本教程: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以这种方式录制视频?
我用Swift 5 AVFoundation编写了示例项目。一个控制器中的照片和摄像机。在前后摄像头之间具有切换器。通过this link进行检查>