AVCaptureSession canAddInput在Swift 3上返回false

时间:2018-12-18 01:54:00

标签: swift avfoundation avcapturesession

我正在尝试使用Swift 3中的AVCaptureSession捕获摄像机视频帧。在授予摄像机权限后,canAddInput返回一个错误值。我看不到这里有什么问题。 canAddInput函数返回错误值的原因可能是什么?

private var position = AVCaptureDevicePosition.front
func configureSession() -> Bool? {
    var captureSession = AVCaptureSession()
    captureSession.sessionPreset = AVCaptureSessionPresetLow
    let cameraString = "back"
    switch cameraString {
    case "back":
        position = AVCaptureDevicePosition.back
    default:
        position = AVCaptureDevicePosition.front
    }
    let camera = selectCaptureDevice()
    guard let captureDeviceInput = try? AVCaptureDeviceInput(device: camera) else { errback(str: "Input couldnt be created"); return false }
    if !captureSession.canAddInput(captureDeviceInput) {
      NSLog("Input cant be added. First attempt")
    }
}
private func selectCaptureDevice() -> AVCaptureDevice? {
    return AVCaptureDevice.devices().filter {
        ($0 as AnyObject).hasMediaType(AVMediaTypeVideo) &&
            ($0 as AnyObject).position == position
        }.first as? AVCaptureDevice
}

0 个答案:

没有答案