AVFoundation:由于"没有活动/启用连接而开始录制崩溃"

时间:2016-10-10 08:13:49

标签: swift camera avfoundation

我试图在Swift中创建自定义相机。我发现了很多帖子,并设法打开相机,开始录制"按钮。但是,当点击"开始录制"按钮,我有这个错误:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p></p>
<div></div>

当查找此错误时,它应该是由于我没有设置我在 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] No active/enabled connections' 功能中所做的相机质量造成的:

beginSession

以下是完整代码:

func beginSession(captureDevice: AVCaptureDevice) {
        var err : NSError? = nil

        var input = AVCaptureDeviceInput?()
        do {
            input = try AVCaptureDeviceInput(device: captureDevice)
        } catch _ {
            //Error handling, if needed
        }
         captureSession.addInput(input)
         captureSession.sessionPreset = AVCaptureSessionPresetHigh;

        if err != nil {
            print("error: \(err?.localizedDescription)")
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.view.layer.addSublayer(previewLayer!)
        previewLayer?.frame = self.view.layer.frame
        captureSession.startRunning()
        let btn: UIButton = UIButton(frame: CGRectMake((self.view.frame.size.width-350)/2, (self.view.frame.size.height-80), 350, 70))
        btn.backgroundColor = hexStringToUIColor("#E53935")
        btn.setTitle("Start Recording", forState: UIControlState.Normal)
        btn.addTarget(self, action: "takeVideoAction", forControlEvents: UIControlEvents.TouchUpInside)
        btn.tag = 1               // change tag property
        self.view.addSubview(btn)

    }

非常感谢任何输入

1 个答案:

答案 0 :(得分:4)

您需要在调用startRecordingToOutputFileURL 之前将视频文件输出添加到捕捉会话

self.captureSession.addOutput(videoFileOutput)
videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate)