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

后端 未结 11 1398
闹比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:09

    Here's my code (Swift 3) to get camera position :

    // Find a camera with the specified AVCaptureDevicePosition, returning nil if one is not found
    func cameraWithPosition(_ position: AVCaptureDevicePosition) -> AVCaptureDevice?
    {
        if let deviceDescoverySession = AVCaptureDeviceDiscoverySession.init(deviceTypes: [AVCaptureDeviceType.builtInWideAngleCamera],
                                                              mediaType: AVMediaTypeVideo,
                                                              position: AVCaptureDevicePosition.unspecified) {
    
            for device in deviceDescoverySession.devices {
                if device.position == position {
                    return device
                }
            }
        }
    
        return nil
    }
    

    If you want, you can also get the new devicesTypes from iPhone 7+ (dual camera) by changing the deviceTypes array.

    Here's a good read : https://forums.developer.apple.com/thread/63347

提交回复
热议问题