解决方案
以下是在 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.