无法设置视频录制的最长持续时间?

时间:2018-09-23 18:45:45

标签: ios swift avfoundation swift4

我创建了一个自定义相机,希望将最大录制时间设置为30秒。这是设置最大值的代码。

@objc func videoAction(sender: UIButton){

    if(imageSetAction()){
        videoImage.image = UIImage(named: "video")
        flashButton.isHidden = true
        videoLabel.textColor = ConstantColors.selectedTextColor
        currentSelected = sender.tag
        if(videoButton.isEnabled){
            if(photoOutput != nil){
                captureSession.removeOutput(photoOutput!)
            }

            self.movieFileOutput = AVCaptureMovieFileOutput()
            self.movieFileOutput?.maxRecordedDuration = CMTime(seconds: 30, preferredTimescale: 600)
            if captureSession.canAddOutput(movieFileOutput!) {
                captureSession.addOutput(movieFileOutput!)
            }
            captureSession.commitConfiguration()
            captureSession.sessionPreset = AVCaptureSession.Preset.high
        }
        let longPressGesture = UILongPressGestureRecognizer.init(target: self, action: #selector(handleLongPress))
        self.semiCircleView.addGestureRecognizer(longPressGesture);
        videoButton.isEnabled = false
    }
}

同样,我有一个选项供用户编辑视频,所以我在委托方法中调用默认的视频编辑器。

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
    if error == nil {
        //UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.path, nil, nil, nil)
        if UIVideoEditorController.canEditVideo(atPath: outputFileURL.path){
            let videoEditorController = UIVideoEditorController()
            videoEditorController.delegate = self
            videoEditorController.videoPath = outputFileURL.path
            videoEditorController.modalPresentationStyle = .popover
            videoEditorController.popoverPresentationController?.sourceView = self.view
            present(videoEditorController, animated: true, completion: nil)
        }
    }
    print("completed")
}

func fileOutput(_ output: AVCaptureFileOutput, didStartRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {
    movieFileOutput?.maxRecordedDuration = CMTimeMake(30, 1)

    /* After 30 seconds, let's stop the recording process */
    DispatchQueue.main.asyncAfter(deadline: .now() + 30.0, execute: {
        debugPrint("longpress ended")
        self.movieFileOutput?.stopRecording()
        self.removeProgressBar()
    })
}

不知道为什么,但是视频只录制了10秒钟。任何帮助表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:1)

从Apple文档中获取有关maxRecordedDuration方法的信息。

  

此属性指定对录制持续时间的硬限制   文件。当达到限制并且   fileOutput(_:didFinishRecordingTo:from:error :)委托方法是   调用时出现适当的错误。此属性的默认值   无效,表示没有限制。

如果要在fileOutput(_:didFinishRecordingTo:from:error :)方法中停止录制,也许您不应该停止录制自己。

答案 1 :(得分:0)

达到maximumDuration时将调用Delegate方法,但错误不会为零

func fileOutput(_输出:AVCaptureFileOutput,didFinishRecordingTo outputFileURL:URL,来自连接:[AVCaptureConnection],错误:错误?)