我已经创建了一个自定义相机,我想将最大录制时间设置为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秒。任何帮助都是非常感谢的。谢谢。
发布于 2018-09-23 19:12:11
来自苹果公司关于maxRecordedDuration方法的文档。
此属性指定记录文件持续时间的硬限制。达到限制时停止记录,并以适当的错误调用fileOutput(_:didFinishRecordingTo:from:error:)委托方法。此属性的默认值无效,表示无限制。
也许您不应该停止录制自己,如果它将停止在fileOutput(_:didFinishRecordingTo:from:error:)方法。
发布于 2020-04-15 14:45:04
到达maximumDuration时将调用委托方法,但错误不会为零。
函数fileOutput(_ output: AVCaptureFileOutput,didFinishRecordingTo outputFileURL: URL,from connections: AVCaptureConnection,error: Error?)
https://stackoverflow.com/questions/52469187
复制相似问题