Search code examples
iosswiftavcapturesessionavcapturedevice

Can a AVCaptureFileOutputRecordingDelegate be added to a subclass UIView?


I am having an issue creating a capture session in a custom UIView. I set the delegate like this

class Camera: UIView, AVCaptureFileOutputRecordingDelegate, AVAudioRecorderDelegate {
}

and then I set everything up and set the delegate like this

self.recordingDelegate? = self

    captureSession.sessionPreset = AVCaptureSessionPresetHigh
    let devices = AVCaptureDevice.devices()
    for device in devices {
        if (device.hasMediaType(AVMediaTypeVideo)) {
            if(device.position == AVCaptureDevicePosition.Back) {
                captureDevice = device as? AVCaptureDevice
                if captureDevice != nil {
                    beginSession()
                }
            }
        }
    }

and all goes well. However, in the beginSession function:

    func beginSession() {
        let err : NSError? = nil
        do {
            self.captureSession.addInput(try AVCaptureDeviceInput(device: self.captureDevice!))
        }
        catch {
            print("dang")
        }
        if err != nil {
            print("error: \(err?.localizedDescription)")
        }

...

The catch is thrown when I try to add the capture device input and there for it is not being added and I can not figure out why.

All of my code I am currently using was working fine before when I had it inside a UIViewController but when I switched it over to a subclass of UIView it stopped working. Any help would be appreciated if more code is needed let me know thank you!


Solution

  • I figured it out the iOS device I was using did not have the camera enabled for some reason there for the input could not be added which made the preview layer unable to capture any data