如何使用AVCaptureDeviceDiscoverySession获取前置摄像头,后置摄像头和音频 [英] How to get front camera, back camera and audio with AVCaptureDeviceDiscoverySession

查看:1669
本文介绍了如何使用AVCaptureDeviceDiscoverySession获取前置摄像头,后置摄像头和音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 10问世之前,我使用以下代码为我的录像机拍摄视频和音频:

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
         }
     }
 }

iOS 10终于来了我运行代码时收到以下警告。请注意,我的录像机仍然可以平稳运行大约2周。

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.


在iOS 10.0中不推荐使用'devices()':使用AVCaptureDeviceDiscoverySession代替。

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

当我今天早上运行我的代码时,我的录像机停止工作了。 xCode8没有给我任何错误,但摄像头捕获的previewLayer是完全白色的。当我开始录制时,我收到以下错误:

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:


错误域= AVFoundationErrorDomain代码= -11800操作无法完成UserInfo = {NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x17554440 {错误域= NSOSStatusErrorDomain代码= -12780(null)},NSLocalizedFailureReason =发生未知错误(-12780)}

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)}

我认为这与我使用弃用方法 AVCaptureDevice.devices()的事实有关。因此,我想知道如何使用 AVCaptureDeviceDiscoverySession

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?

提前感谢您的帮助!

推荐答案

您可以通过以下方式获得前置摄像头:

You can get the front camera with the following:

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

后置摄像头:

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

麦克风:

AVCaptureDevice.default(.builtInMicrophone, for: AVMediaType.audio, position: .unspecified)

这篇关于如何使用AVCaptureDeviceDiscoverySession获取前置摄像头,后置摄像头和音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆