繁体   English   中英

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

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

任何人都知道是否可以使用 AVCaptureDevice.DiscoverySession 来检测任何连接的相机或麦克风,而无需遍历每种不同类型,检查它们并将它们附加到数组中?

例如,我用来检测连接的摄像头或麦克风的方法是使用这样的 for 循环,但现在这种方法已被弃用,我很好奇他们的新 AVCaptureDevice.DiscoverySession 方法是否有解决方案。

//旧的方式是这样的:

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

//新的方式是这样的:

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

//但是我怎么能??

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

解决方案

以下是使用 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
    }

iPhone 12 Pro Max 上的输出

  • getListOfCamerasOnTheDevice() -
    • 后置摄像头
    • 前置摄像头
    • 后置长焦相机
  • getListOfMicrophonesOnTheDevice()
    • iPhone 麦克风”

所有选项

当然,还有更多的 AVCaptureDevice 设备类型。 这是来自Apple 官方文档的当前列表。

相机

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)

麦克风

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

外部设备

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

桌面视图

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

深度感应(测试版)

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