How to get front camera, back camera and audio with AVCaptureDeviceDiscoverySession

后端 未结 11 1420
闹比i
闹比i 2020-12-12 20:58

Before iOS 10 came out I was using the following code to get the video and audio capture for my video recorder:

 for device in AVCaptureDevice.devices()
 {
          


        
11条回答
  •  余生分开走
    2020-12-12 21:18

    Simplified:

    func getCamera(with position: AVCaptureDevice.Position) -> AVCaptureDevice? {
        let deviceDescoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: .video, position: .unspecified)
        return deviceDescoverySession.devices.first(where: { $0.position == position })
    }
    
    // and you can use like that:
    
    guard let device = getCamera(with: .back) else { return }
    

提交回复
热议问题