I'm getting an error saying:
cannot instantiate a AVCaptureDevice directly
When I'm trying to set the camera to the front camera. My code looks like this:
let inputDevice:AVCaptureDevice = setCameraPosition()
var error:NSError?
let deviceInput:AVCaptureDeviceInput = AVCaptureDeviceInput.deviceInputWithDevice(inputDevice, error: &error) as AVCaptureDeviceInput
func setCameraPosition()->AVCaptureDevice{
let devices = AVCaptureDevice.devices()
var captureDevice = AVCaptureDevice()
// Loop through all the capture devices on this phone
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(device.position == AVCaptureDevicePosition.Front) {
captureDevice = device as AVCaptureDevice
}
}
}
return captureDevice
}
I am able to use the back camera by using this line of code:
let inputDevice:AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
and I am not able to figure out where it goes wrong.
Any suggestions on how to proceed would be appreciated.