我有以下代码: 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
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; } }
警告说不支持devicesWithMediaType,我应该使用AVCaptureDeviceDiscoverySession,我尝试了以下内容:
AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack]; NSArray *captureDevices = [captureDeviceDiscoverySession devices];
但是设备阵列只有我的后置摄像头而不是我的前置摄像头,有什么帮助吗?
从 documentation起Pass AVCaptureDevicePositionUnspecified to search for devices
regardless of position.
因此,使用AVCaptureDevicePositionUnspecified来查找所有捕获设备.
所以代码看起来像这样:
AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position: AVCaptureDevicePositionUnspecified]; // here you pass AVCaptureDevicePositionUnspecified to find all capture devices NSArray *captureDevices = [captureDeviceDiscoverySession devices];