2017-08-29 155 views
0

我有以下代碼:devicesWithMediaType已被棄用

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]; 

但數組設備只有我的背部相機而不是我的前置攝像頭,有幫助嗎?

+0

*,但設備的陣列只有我的後置攝像頭,而不是我的前凸輪* - 這是可以預料的,當你初始化'AVCaptureDeviceDiscoverySession'用'AVCaptureDevicePositionBack' –

回答

2

documentation

通行證AVCaptureDevicePositionUnspecified搜索設備 與位置無關。

因此,使用AVCaptureDevicePositionUnspecified來查找所有捕獲設備。

因此,代碼如下所示:

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

NSArray *captureDevices = [captureDeviceDiscoverySession devices];