How to get front camera, back camera and audio with AVCaptureDeviceDiscoverySession

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

Before iOS 10 came out I was using the following code to get the video and audio capture for my video recorder:

 for device in AVCaptureDevice.devices()  {      if (device as AnyObject).hasMediaType( AVMediaTypeAudio )      {          self.audioCapture = device as? AVCaptureDevice      }      else if (device as AnyObject).hasMediaType( AVMediaTypeVideo )      {          if (device as AnyObject).position == AVCaptureDevicePosition.back          {              self.backCameraVideoCapture = device as? AVCaptureDevice          }          else          {              self.frontCameraVideoCapture = device as? AVCaptureDevice          }      }  }

When iOS 10 finally came out, I received the following warning when I was running my code. Note that my video recorder was still working smoothly for about 2 weeks.

'devices()' was deprecated in iOS 10.0: Use AVCaptureDeviceDiscoverySession instead.

As I was running my code this morning, my video recorder stopped working. xCode8 does not give me any errors but the previewLayer for the camera capture is completely white. When I then start recording I receive the following error:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x17554440 {Error Domain=NSOSStatusErrorDomain Code=-12780 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12780)}

I believe that has something to do with the fact that I am using the deprecated approach AVCaptureDevice.devices(). Hence, I was wondering how to use AVCaptureDeviceDiscoverySession instead?

Thank you for your help in advance!

回答1:

You can get the front camera with the following:

AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)

The back camera:

AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .back)

And the microphone:

AVCaptureDevice.defaultDevice(withDeviceType: .builtInMicrophone, mediaType: AVMediaTypeAudio, position: .unspecified)


回答2:

Here's my code (Swift 3) to get camera position :

// Find a camera with the specified AVCaptureDevicePosition, returning nil if one is not found func cameraWithPosition(_ position: AVCaptureDevicePosition) -> AVCaptureDevice? {     if let deviceDescoverySession = AVCaptureDeviceDiscoverySession.init(deviceTypes: [AVCaptureDeviceType.builtInWideAngleCamera],                                                           mediaType: AVMediaTypeVideo,                                                           position: AVCaptureDevicePosition.unspecified) {          for device in deviceDescoverySession.devices {             if device.position == position {                 return device             }         }     }      return nil }

If you want, you can also get the new devicesTypes from iPhone 7+ (dual camera) by changing the deviceTypes array.

Here's a good read : https://forums.developer.apple.com/thread/63347



回答3:

Swift 4, iOS 10+ and Xcode 9 beta 5 replaces

if let cameraID = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)?.localizedName {        //cameraID = "Front Camera" }

with AVCaptureDevice.DiscoverySession implementation

if let cameraID = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front).devices.first?.localizedName{        //cameraID = "Front Camera" } 

Need to wrap it with #available(iOS 10,*) check.



回答4:

It works on Xcode 9.2 and Swift 4

AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

https://developer.apple.com/documentation/avfoundation/avcapturedevice/2361508-default



回答5:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!