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

Cannot set maximum duration for video recording?

我创建了一个自定义相机,我想将最长录制时间设置为 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 秒。任何帮助表示赞赏。谢谢

来自关于 maxRecordedDuration 方法的 Apple 文档。

This property specifies a hard limit on the duration of recorded files. Recording is stopped when the limit is reached and the fileOutput(_:didFinishRecordingTo:from:error:) delegate method is invoked with an appropriate error. The default value of this property is invalid, which indicates no limit.

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

委托方法会在达到maximumDuration时被调用,但error不会为nil

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