Question - AVCaptureDevice select front camera

less than 1 minute read

How can I select the front camera?

From video Live camera feed in SwiftUI with AVCaptureVideoPreviewLayer

Answer

In file ViewController the AVCaptureDevice is selected. For this, we create a device and add it to the existing capture session.

guard let videoDevice = AVCaptureDevice.default(.builtInDualWideCamera,for: .video, position: .back) else { return }
guard let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice) else { return }
guard captureSession.canAddInput(videoDeviceInput) else { return }

Here, we search for a .builtInDualWideCamera back camera and if it exists we use it. The position is an enum with the three values .back, .front, .unspecified, see here.

Another way to select input devices is to use the DiscoverySession, see here.