【问题标题】:Detect all Cameras and Mics in iOS 15+在 iOS 15+ 中检测所有相机和麦克风
【发布时间】:2022-07-01 23:31:37
【问题描述】:

任何人都知道是否可以使用 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)

【问题讨论】:

  • 嘿@Dave Levy,请在下面查看我的答案。

标签: swift camera microphone detect


【解决方案1】:

解决方案

以下是在 iOS 上使用 Swift 获取相机和麦克风的方法:

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() - [AnyHashable("Back Camera"), AnyHashable("Front Camera"), AnyHashable("Back Telephoto Camera")]

  • getListOfMicrophonesOnTheDevice() - [AnyHashable("iPhone Microphone")]

所有选项

当然,还有更多的 AVCaptureDevice 设备类型。这是来自the official Apple documentation 的当前列表。

相机

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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2015-11-26
    • 2011-09-15
    相关资源
    最近更新 更多