简体   繁体   English

在 iOS 15+ 中检测所有相机和麦克风

[英]Detect all Cameras and Mics in iOS 15+

Anyone know if it is possible to use AVCaptureDevice.DiscoverySession to detect any Camera or Mic connected without just going through each of the different types, checking for them, and appending them to an array?任何人都知道是否可以使用 AVCaptureDevice.DiscoverySession 来检测任何连接的相机或麦克风,而无需遍历每种不同类型,检查它们并将它们附加到数组中?

For instance, the way I used to detect connected cameras or microphones was with a for loop like this, but now that way of doing it is deprecated and I'm curious if there is a solution with their new AVCaptureDevice.DiscoverySession method.例如,我用来检测连接的摄像头或麦克风的方法是使用这样的 for 循环,但现在这种方法已被弃用,我很好奇他们的新 AVCaptureDevice.DiscoverySession 方法是否有解决方案。

//THE OLD WAY WAS LIKE: //旧的方式是这样的:

for eachDevice in AVCaptureDevice.devices() {print(eachDevice)}

//THE NEW WAY IS LIKE: //新的方式是这样的:

let discoverFrontFacingWideAngleCamerasConnected = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .front)

for device in discoverFrontFacingWideAngleCamerasConnected.devices {
    print("there is a front facing wide angle camera named -> \(device.localizedName)")
} 

//BUT HOW CAN I ?? //但是我怎么能??

let allCamerasAndMicrophonesConnected = AVCaptureDevice.DiscoverySession.init(ANY CAMERAS OR MICS)

Solution解决方案

Here is how you can get Cameras and Microphones on iOS with Swift:以下是使用 Swift 在 iOS 上获取相机和麦克风的方法:

public func getListOfCamerasOnTheDevice() -> [AnyHashable] {
        let session = AVCaptureDevice.DiscoverySession(
            deviceTypes: [
                .builtInWideAngleCamera,
                .builtInTelephotoCamera,
                .builtInMicrophone
            ],
            mediaType: .video,
            position: .unspecified)

        let captureDevices = session.devices
        var names: [AnyHashable] = []
        for device in captureDevices {
            guard let device = device as? AVCaptureDevice else {
                continue
            }
            
            names.append(device.localizedName)
        }
        
        return names
    }
    
    public func getListOfMicrophonesOnTheDevice() -> [AnyHashable] {
        let session = AVCaptureDevice.DiscoverySession(
            deviceTypes: [
                .builtInMicrophone
            ],
            mediaType: .audio,
            position: .unspecified)

        let captureDevices = session.devices
        var names: [AnyHashable] = []
        
        for device in captureDevices {
            guard let device = device as? AVCaptureDevice else {
                continue
            }
            
            names.append(device.localizedName)
        }
        
        return names
    }

Outputs on iPhone 12 Pro Max iPhone 12 Pro Max 上的输出

  • getListOfCamerasOnTheDevice() - getListOfCamerasOnTheDevice() -
    • Back Camera后置摄像头
    • Front Camera前置摄像头
    • Back Telephoto Camera后置长焦相机
  • getListOfMicrophonesOnTheDevice() getListOfMicrophonesOnTheDevice()
    • iPhone Microphone" iPhone 麦克风”

All Options所有选项

Sure, there are more AVCaptureDevice device types.当然,还有更多的 AVCaptureDevice 设备类型。 Here is the current list from the official Apple documentation .这是来自Apple 官方文档的当前列表。

Cameras相机

static let builtInWideAngleCamera: AVCaptureDevice.DeviceType // A built-in wide-angle camera.

static let builtInUltraWideCamera: AVCaptureDevice.DeviceType // A built-in camera with a shorter focal length than that of the wide-angle camera.

static let builtInTelephotoCamera: AVCaptureDevice.DeviceType // A built-in camera device with a longer focal length than the wide-angle camera.

static let builtInDualCamera: AVCaptureDevice.DeviceType // A device that consists of a wide-angle and telephoto camera.

static let builtInDualWideCamera: AVCaptureDevice.DeviceType // A device that consists of two cameras of fixed focal length, one ultrawide angle and one wide angle.

static let builtInTripleCamera: AVCaptureDevice.DeviceType // A device that consists of three cameras of fixed focal length, one ultrawide angle, one wide angle, and one telephoto.

static let builtInDuoCamera: AVCaptureDevice.DeviceType // A built-in dual camera device. (Deprecated)

Microphones麦克风

static let builtInMicrophone: AVCaptureDevice.DeviceType // A built-in microphone.

External Devices外部设备

static let externalUnknown: AVCaptureDevice.DeviceType // An unknown external device type.

Desk View桌面视图

static let deskViewCamera: AVCaptureDevice.DeviceType // A virtual overhead camera that captures a user’s desk.

Depth Sensing (Beta)深度感应(测试版)

static let builtInLiDARDepthCamera: AVCaptureDevice.DeviceType // A device that consists of two cameras, one LiDAR and one YUV.

static let builtInTrueDepthCamera: AVCaptureDevice.DeviceType // A device that consists of two cameras, one Infrared and one YUV.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM