4

I have the following code:

NSArray * devices = [ AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo ];

// 2. Iterate through the device array and if a device is a camera, check if it's the one we want:
for ( AVCaptureDevice * device in devices )
{
    if ( useFrontCamera && AVCaptureDevicePositionFront == [ device position ] )
    {
        // We asked for the front camera and got the front camera, now keep a pointer to it:
        m_camera = device;
    }
    else if ( !useFrontCamera && AVCaptureDevicePositionBack == [ device position ] )
    {
        // We asked for the back camera and here it is:
        m_camera = device;
    }
}

the warning says that devicesWithMediaType is deprecated and I should use AVCaptureDeviceDiscoverySession instead, I have tried the following:

AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] 
                                      mediaType:AVMediaTypeVideo 
                                       position:AVCaptureDevicePositionBack];
NSArray *captureDevices = [captureDeviceDiscoverySession devices];

but the array of devices has only my back camera and not my front cam, any help?

MLavoie
  • 9,671
  • 41
  • 36
  • 56
user3411226
  • 183
  • 1
  • 14
  • *but the array of devices has only my back camera and not my front cam* - that's to be expected when you initialize `AVCaptureDeviceDiscoverySession` with `AVCaptureDevicePositionBack` – mag_zbc Aug 29 '17 at 08:32
  • Does this answer your question? [IOS devicesWithMediaType deprecated](https://stackoverflow.com/questions/44735554/ios-deviceswithmediatype-deprecated) – Lal Krishna Aug 14 '20 at 04:52

1 Answers1

9

From documentation

Pass AVCaptureDevicePositionUnspecified to search for devices regardless of position.

So use AVCaptureDevicePositionUnspecified to find all capture devices.

So the code will look like this:

AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] 
                                      mediaType:AVMediaTypeVideo 
                                       position: AVCaptureDevicePositionUnspecified]; // here you pass AVCaptureDevicePositionUnspecified to find all capture devices

NSArray *captureDevices = [captureDeviceDiscoverySession devices];
Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54