Type Method

default(_:for:position:)

Returns the default device for the specified device type, media type, and position.

Declaration

class func `default`(_ deviceType: AVCaptureDevice.DeviceType, for mediaType: AVMediaType?, position: AVCaptureDevice.Position) -> AVCaptureDevice?

Parameters

deviceType

The type of capture device to request, such as builtInWideAngleCamera. See AVCaptureDevice.DeviceType.

mediaType

The type of media to request capture of, such as audio. See Media Types.

position

The position of capture device to request relative to system hardware (front- or back-facing). Pass AVCaptureDevice.Position.unspecified to search for devices regardless of position.

Return Value

The system default device for the specified combination of device type, media type, and position, or nil if no currently available device satisfies the specified criteria.

Discussion

Use this method to easily select the system default capture device for a given scenario. For example, to obtain the dual camera on supported hardware and fall back to the standard wide-angle camera otherwise, call this method twice, as shown below.

func defaultCamera() -> AVCaptureDevice? {
    if let device = AVCaptureDevice.default(.builtInDualCamera,
                                            for: AVMediaType.video,
                                            position: .back) {
        return device
    } else if let device = AVCaptureDevice.default(.builtInWideAngleCamera,   
                        for: AVMediaType.video,
                        position: .back) {
        return device
    } else {
        return nil
    }
}

See Also

Discovering Devices

class AVCaptureDevice.DiscoverySession

A query for finding and monitoring available capture devices.

class func devices() -> [AVCaptureDevice]

Returns an array of the available capture devices on the system.

init?(uniqueID: String)

Returns the device with a given ID.

class func `default`(for: AVMediaType) -> AVCaptureDevice?

Returns the default device used to capture data of a given media type.

class func devices(for: AVMediaType) -> [AVCaptureDevice]

Returns an array of the devices able to capture data of a given media type.

struct AVCaptureDevice.DeviceType

Values identifying the general type of a capture device, used with the default(_:for:position:) method and the AVCaptureDevice.DiscoverySession class.

enum AVCaptureDevice.Position

Constants to specify the position of a capture device.

static let AVCaptureDeviceWasConnected: NSNotification.Name

Posted when a new device becomes available.

static let AVCaptureDeviceWasDisconnected: NSNotification.Name

Posted when an existing device becomes unavailable.