i use this code in my app
// MARK: - Protocol
protocol VideoManagerProtocol: AnyObject {
func didReceive(sampleBuffer: CMSampleBuffer)
}
final class VideoManager: NSObject {
// MARK: -- Properties
/// RequestPermissionCompletionHandler
typealias RequestPermissionCompletionHandler = ((_ accessGranted: Bool) -> Void)
/// delegate of VideoManager
weak var delegate: VideoManagerProtocol?
/// An object that manages capture activity.
let captureSession = AVCaptureSession()
/// A device that provides input (such as audio or video) for capture sessions.
// let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) // choose deviceType, position is better but don't know ipad camera so choose default saftely
let videoDevice = AVCaptureDevice.default(for: .video)
/// A Core Animation layer that displays the video as it’s captured.
lazy var videoLayer: AVCaptureVideoPreviewLayer = {
return AVCaptureVideoPreviewLayer(session: captureSession)
}()
/// A capture output that records video and provides access to video frames for processing.
lazy var videoOutput: AVCaptureVideoDataOutput = {
let output = AVCaptureVideoDataOutput()
let queue = DispatchQueue(label: "VideoOutput", attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit)
output.setSampleBufferDelegate(self, queue: queue)
output.alwaysDiscardsLateVideoFrames = true
return output
}()
// MARK: -- Methods
override init() {
guard let videoDevice = videoDevice, let videoInput = try? AVCaptureDeviceInput(device: videoDevice) else {
fatalError("No `Video Device` detected!")
}
super.init()
captureSession.addInput(videoInput)
captureSession.addOutput(videoOutput)
}
func startVideoCapturing() {
self.captureSession.startRunning() // do not operate in dispatch global background
// DispatchQueue.global(qos: .background).async {
// self.captureSession.startRunning()
// }
}
func stopVideoCapturing() {
captureSession.stopRunning()
}
func requestPermission(completion: @escaping RequestPermissionCompletionHandler) {
AVCaptureDevice.requestAccess(for: .video) { (accessGranted) in
completion(accessGranted)
}
}
}
extension VideoManager {
}
// MARK: - AVCaptureVideoDataOutputSampleBufferDelegate
extension VideoManager: AVCaptureVideoDataOutputSampleBufferDelegate {
func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
delegate?.didReceive(sampleBuffer: sampleBuffer)
}
}
i tried like this AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
The camera uses only the front camera. how to use back camera?