从前置摄像头切换到后置摄像头:目前不支持多个音频/视频AVCaptureInput

时间:2017-01-24 07:32:51

标签: ios swift xcode camera

我正在尝试将相机从正面切换到背面,反之亦然。但是代码提供异常“当前不支持多个音频/视频AVCaptureInput”并且我的应用程序随机崩溃。

func addVideoInput() {
    let device: AVCaptureDevice = self.deviceWithMediaTypeWithPosition(AVMediaTypeVideo, position: AVCaptureDevicePosition.Front)
    do {
        let input = try AVCaptureDeviceInput(device: device)
        if self.session.canAddInput(input) {
            self.session.removeInput(input)
            if session.inputs.isEmpty {
                self.session.addInput(input)
            }
        }
    } catch {
        print(error)
    }

在backButton上调用方法点击

func initilizeBackCamera() -> Bool {

    var success  = false

    let device: AVCaptureDevice = (self.deviceWithMediaTypeWithPosition(AVMediaTypeVideo, position: AVCaptureDevicePosition.Back))
    do {
        let input = try AVCaptureDeviceInput(device: device)
         self.session.removeInput(input)
        if session.canAddInput(input) {
            if self.session.inputs.isEmpty {
              self.session.addInput(input)
                success = true
            }
        }

    } catch {

        print(error)
        success = false
    }

    return success;
}

在frobtButton上调用方法点击

 func initilizeFrontCamera() -> Bool {

    var success  = false
    let device: AVCaptureDevice = (self.deviceWithMediaTypeWithPosition(AVMediaTypeVideo, position: AVCaptureDevicePosition.Front))
    do {
        let input = try AVCaptureDeviceInput(device: device)

         self.session.removeInput(input)
        if session.canAddInput(input) {
            if self.session.inputs.isEmpty {
                self.session.addInput(input)
                success = true
            }

        }

    } catch {

        print(error)
         success = false
    }
     return success;
}

0 个答案:

没有答案