swiftcameramicrophonedetect

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?

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.

//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

  • Solution

    Here is how you can get Cameras and Microphones on iOS with Swift:

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

    Outputs on iPhone 12 Pro Max

    All Options

    Sure, there are more AVCaptureDevice device types. Here is the current list from the official Apple documentation.

    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.