Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'AVCaptureDevice' has no member 'defaultDevice'

Working on a QR code reader. I am new to programming so this might be an easy fix. The error is "Type 'AVCaptureDevice' has no member 'defaultDevice'" Thanks for the help in advance!

 //Creating session
    let session = AVCaptureSession()
    //Define capture device
    let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

    do
    {
        let input = try AVCaptureDeviceInput(device: captureDevice)
        session.addInput(input)
    }
like image 890
Brandon Ruetsche Avatar asked Oct 26 '17 02:10

Brandon Ruetsche


People also ask

What is an avcapturedevice object?

An AVCaptureDevice object represents a physical capture device and the properties associated with that device. You use a capture device to configure the properties of the underlying hardware. A capture device also provides input data (such as audio or video) to an AVCaptureSession object.

What is the default device for the mediatype?

The default device for the mediaType, which should be a value from AVMediaType. An array of available AVCaptureDevice s that can capture mediaType. The device whose UniqueID matches deviceUniqueID. Indicates a change occurred to the indexes for a to-many relationship.

What does lockforconfiguration (nserror) do in avcapturedevice?

Whether the AVCaptureDevice supports the preset. Returns a string representation of the value of the current instance. Obsolete. With LockForConfiguration (NSError), commits the requested configuration changes.

What does the avmediatypetoken do?

Whether the app is permitted to capture the avMediaTypeToken. Gets the device-independent chromaticity values for the device-specific white balance RGB gains.. Returns the default device for the provided device and media types and front or back facing position. Returns the default device for the provided media type.


2 Answers

You are using the old Swift 2 API. The line:

let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

should be:

let captureDevice = AVCaptureDevice.default(for: .video)
like image 197
rmaddy Avatar answered Sep 21 '22 10:09

rmaddy


This is swift 3.0

let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

In swift 4.1

AVCaptureDevice.default(for: AVMediaType.video)
{
   let input = AVCaptureDeviceInput(device: captureDevice)
   session.addInput(input)
}

I hope this will help you

like image 44
Raja Usman Avatar answered Sep 20 '22 10:09

Raja Usman