检测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上获得相机和麦克风:

/// Returns all cameras on the device.
public func getListOfCameras() -> [AVCaptureDevice] {

#if os(iOS)
let session = AVCaptureDevice.DiscoverySession(
deviceTypes: [
.builtInWideAngleCamera,
.builtInTelephotoCamera
],
mediaType: .video,
position: .unspecified)
#elseif os(macOS)
let videoDeviceDiscoverySession = AVCaptureDevice.DiscoverySession(
deviceTypes: [
.builtInWideAngleCamera
],
mediaType: .video,
position: .unspecified)
#endif

return session.devices
}
/// Returns all microphones on the device.
public func getListOfMicrophones() -> [AVCaptureDevice] {
let session = AVCaptureDevice.DiscoverySession(
deviceTypes: [
.builtInMicrophone
],
mediaType: .audio,
position: .unspecified)

return session.devices
}
/// Converts giving AVCaptureDevice list to the String
public func convertDeviceListToString(_ devices: [AVCaptureDevice]) -> [String] {
var names: [String] = []

for device in devices {
names.append(device.localizedName)
}

return names
}
public func getListOfCamerasAsString() -> [String] {
let devices = getListOfCameras()
return convertDeviceListToString(devices)
}
public func getListOfMicrophonesAsString() -> [String] {
let devices = getListOfMicrophones()
return convertDeviceListToString(devices)
}

iPhone 12 Pro Max上的输出

  • getListOfCamerasOnTheDevice((-
    • 后置摄像头
    • 前置摄像头
    • 背面电话摄像头
  • getListOfMicrophonesThemeDevice((
    • iPhone麦克风">

所有选项

当然,还有更多的AVCaptureDevice设备类型。这是苹果官方文档中的最新列表。

摄像头

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.

深度传感(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.

相关内容

  • 没有找到相关文章

最新更新