AVCaptureDevicePosition problems on iPhone 6s

I am using Jason King’s iOSKit to access an iPhone camera via declares.

Accessing the back camera works fine on an iPhone 6s and on an iPad Mini.

When testing on an iPad Mini, accessing the front camera works fine also.

When testing on an iPhone 6s, there is no device where AVCaptureDevicePosition is equal to Front, even though the iPhone has a working front camera.

Jason has the following method to detect if the front camera exists and to return an AVFoundation.AVCaptureDevice if it does:

dim devices as Foundation.NSArray = AVFoundation.AVCaptureDevice.DevicesWithMediaType(AVFoundation.AVStringConstant("AVMediaTypeVideo"))
  
dim count as Integer = devices.Count
  
 for x as Integer = 0 to count-1
    dim device as new AVFoundation.AVCaptureDevice(devices.Value(x))
    
    if device.position = AVFoundation.AVCaptureDevice.AVCaptureDevicePosition.Front then
      Return device
    end if
  next 
  Return nil

On, an iPad Mini I can see that in the devices array there are are two devices, one where AVCaptureDevicePosition is Back and one where it is Front, as expected.

On an iPhone 6s, there is one device where AVCaputreDevice position is Unspecified and another where the position value is not equal to Unspecified, Front, or Back.

If I cast the enum to an integer for the device with the Unspecified position, I get zero as expected. However, for the other device, I get what appears to be a large randomly changing value. E.g.: 4711025515691140123

I’m new to declares, so I may very well be doing something wrong. However it does seem to me that for some reason AVCaptureDevicePosition is not working as expected on the iPhone 6s. Any help would be greatly appreciated.

Unfortunately I don’t know the cause of this and it is plaguing other members of the forum. I tried to track it down but saw the same random behavior as you and I have no idea how to remedy it. It seems to be a problem with the underlying device, but again I have never been able figure out why it’s not working as expected.

As Jason stated, I have had this issue since ios10 was released. Have yet to come up with a solution.

Thanks, gentlemen.

Certainly a weird issue.

For now, I am able to successfully compare the integer value of the default camera device with the integer value the other device in a given array element and if the they are diffent I can flip the camera. Not an ideal work around it seems, but it works.

[quote=314871:@Gregory Crout]Thanks, gentlemen.

Certainly a weird issue.

For now, I am able to successfully compare the integer value of the default camera device with the integer value the other device in a given array element and if the they are different I can flip the camera. Not an ideal work around it seems, but it works.[/quote]

Interesting. I have been unable to get the app to select the front camera in IOS 10. Can you post an example of how you are able to get the camera to flip?

Stephen - maybe we can change the FaceCamera method so that if there are two camera present, it will return the one which is not the back camera? It doesn’t make sense to me why that would be the case, but maybe that is what we have to do in order to make it work properly.

Pretty much what Jason is suggesting also, but here you go (all w/in the FaceCamera method):

  Dim ExistingDevicePositionInteger As Integer
  ExistingDevicePositionInteger = Integer(captureDevice.position)

  for x as Integer = 0 to count-1
  dim device as new AVFoundation.AVCaptureDevice(devices.Value(x))
    
    Dim ArrayDevicePositionInteger As Integer
    ArrayDevicePositionInteger = Integer(device.position)
    
    If ExistingDevicePositionInteger <> ArrayDevicePositionInteger Then
      Return device
    End If
    
  next 

Thanks
How have you set ‘captureDevice.position’ ?

[quote=315054:@Gregory Crout]Pretty much what Jason is suggesting also, but here you go (all w/in the FaceCamera method):

[code]
Dim ExistingDevicePositionInteger As Integer
ExistingDevicePositionInteger = Integer(captureDevice.position)

for x as Integer = 0 to count-1
dim device as new AVFoundation.AVCaptureDevice(devices.Value(x))

Dim ArrayDevicePositionInteger As Integer
ArrayDevicePositionInteger = Integer(device.position)

If ExistingDevicePositionInteger <> ArrayDevicePositionInteger Then
  Return device
End If

next
[/code][/quote]

Stephen,

I left out the first two lines in Jason’s FaceCamera method. They are:

  dim devices as Foundation.NSArray = AVFoundation.AVCaptureDevice.DevicesWithMediaType(AVFoundation.AVStringConstant("AVMediaTypeVideo"))
  dim count as Integer = devices.Count

If the existing camera (the camera that is represented by Jason’s captureDevice property of his StillImageView) has a different position than the position of the given camera in the array, then that given AVCapture device is returned by the FaceCamera method. I didn’t really set the position; it is whatever it is. I return the whole camera (AVCapture device) because it is a different camera and I know it has the position I want.

Just for the sake of completeness, all the code is here:



dim devices as Foundation.NSArray = AVFoundation.AVCaptureDevice.DevicesWithMediaType(AVFoundation.AVStringConstant("AVMediaTypeVideo"))
dim count as Integer = devices.Count

  Dim ExistingDevicePositionInteger As Integer
  ExistingDevicePositionInteger = Integer(captureDevice.position)

  for x as Integer = 0 to count-1
  dim device as new AVFoundation.AVCaptureDevice(devices.Value(x))
    
    Dim ArrayDevicePositionInteger As Integer
    ArrayDevicePositionInteger = Integer(device.position)
    
    If ExistingDevicePositionInteger <> ArrayDevicePositionInteger Then
      Return device
    End If
    
Return nil
  next 

The FaceCamera method has a return type of AVFoundation.AVCaptureDevice

I hope I have answered your question. :slight_smile:

When I try to compile the above code I am getting two errors.

ExistingDevicePositionInteger=Integer(captureDevice.position)

Throws error:
Type Mismatch Error Expected Integer, but got enum AVFoundation.AVCaptureDevice.AVCaptureDevicePosition

ArrayDevicePositionInteger=Integer(Device.Position)

Throws the same error.

Does it work with CType?

Yes it likes CTYPE much better. It still is not selecting the Front Camera. I may need to dig back into my code and make sure I didn’t mess something up trying to fix it last time I worked on it. I will report back after I have had a chance to look that over.

Updating that this did resolve my issues so I am able to access the face camera on my Ipad in IOS10 now!