如何获得AVCaptureDeviceDiscoverySession的前置摄像头、后置摄像头和音频

时间:2022-08-25 22:05:45

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

在ios10发布之前,我使用以下代码获取我的录像机的视频和音频捕获:

 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.

当ios10最终发布时,我在运行代码时收到了以下警告。请注意,我的录像机仍然正常工作了大约两周。

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

在iOS 10.0中,“devices()”被弃用:改为使用AVCaptureDeviceDiscoverySession。

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:

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

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

错误域=AVFoundationErrorDomain Code=-11800 "操作无法完成" UserInfo={NSLocalizedDescription=操作无法完成,NSUnderlyingError=0x17554440{错误域=NSOSStatusErrorDomain Code=-12780 "(null)"}, NSLocalizedFailureReason=出现未知错误(-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?

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

Thank you for your help in advance!

提前感谢您的帮助!

8 个解决方案

#1


52  

You can get the front camera with the following:

您可以得到前置摄像头与以下:

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

The back camera:

后面的相机:

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

And the microphone:

和麦克风:

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

#2


9  

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

这是我的密码(Swift 3)以获得相机位置:

// 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.

如果需要,还可以通过更改deviceTypes数组从iPhone 7+(双摄像头)获得新的devicesTypes。

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

这里有一个很好的阅读:https://forums.developer.apple.com/thread/63347。

#3


8  

Swift 4, iOS 10+ and Xcode 9.3 replaces

Swift 4, iOS 10+和Xcode 9.3替换。

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

with AVCaptureDevice.DiscoverySession implementation

AVCaptureDevice。DiscoverySession实现

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

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

需要用#available(ios10,*)检查它。

#4


4  

It works on Xcode 9.2 and Swift 4

它适用于Xcode 9.2和Swift 4

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

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

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

#5


2  

For my video capture app I'm using the following code to get the mic, front and rear camera and I've tested this code from iOS 7 to 10.0.2.

对于我的视频捕捉应用程序,我使用下面的代码来获取麦克风,前后摄像头,我已经测试了这个代码从ios7到10.0。2。

        var frontCamera : AVCaptureDevice?
        var rearCamera : AVCaptureDevice?

        captureSession = AVCaptureSession()

        let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)

        let audioDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeAudio)

        for mic in audioDevices {
            audioDevice = mic as? AVCaptureDevice
            audioCapturePossible = true
        }

        for device in devices {
            if device.position == AVCaptureDevicePosition.Front {
                frontCamera = device as? AVCaptureDevice
                hasFrontCamera = true
            }
            else if device.position == AVCaptureDevicePosition.Back {
                rearCamera = device as? AVCaptureDevice
                hasRearCamera = true
            }

        }

#6


2  

Try below code to get camera ID:

尝试以下代码获取相机ID:

NSString *cameraID = nil;

NSArray *captureDeviceType = @[AVCaptureDeviceTypeBuiltInWideAngleCamera];
AVCaptureDeviceDiscoverySession *captureDevice = 
              [AVCaptureDeviceDiscoverySession 
                discoverySessionWithDeviceTypes:captureDeviceType 
                mediaType:AVMediaTypeVideo 
                position:AVCaptureDevicePositionUnspecified];

cameraID = [captureDevice.devices.lastObject localizedName];

#7


2  

Swift 3

斯威夫特3

For selecting the back camera:(also you can change .back as needed)

选择后置摄像头:(也可以根据需要修改后置)

For selecting another deviceType simple add it inside the [ ] (i.e:

要选择另一个deviceType simple,请将它添加到[](即:

[deviceTypeCamera, AVCaptureDeviceType.builtInMicrophone]

[deviceTypeCamera,AVCaptureDeviceType.builtInMicrophone]

(or create a private let... like I did in the code with the back camera)

(或者创建一个私有的let…就像我用后置摄像头做的代码一样)

 private let position = AVCaptureDevicePosition.back
 private let deviceTypeBackCamera = AVCaptureDeviceType.builtInWideAngleCamera

 private func selectCaptureDevice() -> AVCaptureDevice? {
    return AVCaptureDeviceDiscoverySession(deviceTypes: [deviceTypeBackCamera], mediaType: AVMediaTypeVideo, position: position).devices.first

 }

#8


2  

example: iOS 11 Swift 4

例子:ios11 Swift 4

override func viewDidLoad() {
    super.viewDidLoad()

    // Get the back-facing camera for capturing videos

    // AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
    let deviceDiscoverySession = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

   guard let captureDevice = deviceDiscoverySession else {
       print("Failed to get the camera device")
       return
   }

    do {
        // Get an instance of the AVCaptureDeviceInput class using the previous device object.
        let input = try AVCaptureDeviceInput(device: captureDevice)

        // Set the input device on the capture session.
        captureSession.addInput(input)

    } catch {
        // If any error occurs, simply print it out and don't continue any more.
        print(error)
        return
    }

    // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    videoPreviewLayer?.frame = view.layer.bounds
    view.layer.addSublayer(videoPreviewLayer!)

    // Start video capture.
    captureSession.startRunning()

#1


52  

You can get the front camera with the following:

您可以得到前置摄像头与以下:

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

The back camera:

后面的相机:

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

And the microphone:

和麦克风:

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

#2


9  

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

这是我的密码(Swift 3)以获得相机位置:

// 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.

如果需要,还可以通过更改deviceTypes数组从iPhone 7+(双摄像头)获得新的devicesTypes。

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

这里有一个很好的阅读:https://forums.developer.apple.com/thread/63347。

#3


8  

Swift 4, iOS 10+ and Xcode 9.3 replaces

Swift 4, iOS 10+和Xcode 9.3替换。

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

with AVCaptureDevice.DiscoverySession implementation

AVCaptureDevice。DiscoverySession实现

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

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

需要用#available(ios10,*)检查它。

#4


4  

It works on Xcode 9.2 and Swift 4

它适用于Xcode 9.2和Swift 4

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

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

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

#5


2  

For my video capture app I'm using the following code to get the mic, front and rear camera and I've tested this code from iOS 7 to 10.0.2.

对于我的视频捕捉应用程序,我使用下面的代码来获取麦克风,前后摄像头,我已经测试了这个代码从ios7到10.0。2。

        var frontCamera : AVCaptureDevice?
        var rearCamera : AVCaptureDevice?

        captureSession = AVCaptureSession()

        let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)

        let audioDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeAudio)

        for mic in audioDevices {
            audioDevice = mic as? AVCaptureDevice
            audioCapturePossible = true
        }

        for device in devices {
            if device.position == AVCaptureDevicePosition.Front {
                frontCamera = device as? AVCaptureDevice
                hasFrontCamera = true
            }
            else if device.position == AVCaptureDevicePosition.Back {
                rearCamera = device as? AVCaptureDevice
                hasRearCamera = true
            }

        }

#6


2  

Try below code to get camera ID:

尝试以下代码获取相机ID:

NSString *cameraID = nil;

NSArray *captureDeviceType = @[AVCaptureDeviceTypeBuiltInWideAngleCamera];
AVCaptureDeviceDiscoverySession *captureDevice = 
              [AVCaptureDeviceDiscoverySession 
                discoverySessionWithDeviceTypes:captureDeviceType 
                mediaType:AVMediaTypeVideo 
                position:AVCaptureDevicePositionUnspecified];

cameraID = [captureDevice.devices.lastObject localizedName];

#7


2  

Swift 3

斯威夫特3

For selecting the back camera:(also you can change .back as needed)

选择后置摄像头:(也可以根据需要修改后置)

For selecting another deviceType simple add it inside the [ ] (i.e:

要选择另一个deviceType simple,请将它添加到[](即:

[deviceTypeCamera, AVCaptureDeviceType.builtInMicrophone]

[deviceTypeCamera,AVCaptureDeviceType.builtInMicrophone]

(or create a private let... like I did in the code with the back camera)

(或者创建一个私有的let…就像我用后置摄像头做的代码一样)

 private let position = AVCaptureDevicePosition.back
 private let deviceTypeBackCamera = AVCaptureDeviceType.builtInWideAngleCamera

 private func selectCaptureDevice() -> AVCaptureDevice? {
    return AVCaptureDeviceDiscoverySession(deviceTypes: [deviceTypeBackCamera], mediaType: AVMediaTypeVideo, position: position).devices.first

 }

#8


2  

example: iOS 11 Swift 4

例子:ios11 Swift 4

override func viewDidLoad() {
    super.viewDidLoad()

    // Get the back-facing camera for capturing videos

    // AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
    let deviceDiscoverySession = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

   guard let captureDevice = deviceDiscoverySession else {
       print("Failed to get the camera device")
       return
   }

    do {
        // Get an instance of the AVCaptureDeviceInput class using the previous device object.
        let input = try AVCaptureDeviceInput(device: captureDevice)

        // Set the input device on the capture session.
        captureSession.addInput(input)

    } catch {
        // If any error occurs, simply print it out and don't continue any more.
        print(error)
        return
    }

    // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    videoPreviewLayer?.frame = view.layer.bounds
    view.layer.addSublayer(videoPreviewLayer!)

    // Start video capture.
    captureSession.startRunning()