0

Since AVCaptureDevice.devices is depreciated in iOS 10, I am trying to adjust this example code to AVCaptureDeviceDiscoverySession.

var error: NSError?
var captureSession: AVCaptureSession?
var backVideoDevice: AVCaptureDevice?
//let videoDevices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) // .devices DEPRECIATED

//iOS 10
let videoDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back)

// Get back video device
if let videoDevices = videoDevices
{
    for captureDevice in videoDevices
    {
        if (captureDevice as AnyObject).position == AVCaptureDevicePosition.back
        {
            backVideoDevice = captureDevice as? AVCaptureDevice
            break
        }
    }
}

And here I am stuck, a error comes up at this line

for captureDevice in videoDevices

at the point videoDevices and says: Type 'AVCaptureDeviceDiscoverySession' does not conform to protocol 'Sequence'.

Where or what do I miss or oversee? Thx.

Harry
  • 786
  • 1
  • 8
  • 27

1 Answers1

2

The function returns an object of type AVCaptureDeviceDiscoverySession, and you have to access the devices property of that to get the array you were expecting:

    let session = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back)
    if let device = session?.devices[0] {
        backVideoDevice = device
    }

Note that you no longer need to loop over all devices as the AVCaptureDeviceDiscoverySession is only returning devices with a position of .back in the first place. Since there will be only one of those, you'll find it at devices[0].

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93