我正在尝试使用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
}