正如标题所述,我希望在用户使用 AVFoundation 录制视频时显示当前的录制长度。我对 AVAudioRecorder 做了类似的事情,我创建了一个计时器来设置标签的文本,如下所示:
recordDurationLabel.text = timeString((audioRecorder?.currentTime)!)
我想知道是否有用于录制视频的“当前时间”功能?我唯一的其他想法是在 captureOutput:didStartRecordingToOutputFileAtURL 函数中启动重复的 1 秒计时器,但我想确保准确性。
提前致谢!
编辑: 目前,这是我在 captureOutput:didStartRecordingToOutputFileAtURL 委托(delegate)方法中实现的函数,但同样,拥有与视频直接相关的内容会更清晰。
func startRecordingUpdateTimer() {
recordingUpdateTimer = NSTimer(timeInterval: 1.0/45.0, target: self, selector: #selector(recordedVideo), userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(recordingUpdateTimer!, forMode: NSRunLoopCommonModes)
}
func recordedVideo() {
durationTime = durationTime + 1.0/45.0
let durationString = timeString(durationTime)
recordDurationLabel.text = durationString
}
最佳答案
使用 AVCaptureMovieFileOutput
,您可以使用 recordedDuration
获取电影的持续时间,它是只读的,并会在录制开始时返回 CMTime。请参阅AVCaptureFileOutput .
如果您使用 captureOutput:didOutputSampleBuffer:fromConnection:
,您将收到 CMSampleBuffer
对象,其中包含您可以使用 访问的
。 CMTime
>CMSampleBufferGetPresentationTimeStamp
执行诸如CMTimeSubstract
之类的CMTime操作将允许您计算当前缓冲区与您记录的第一个缓冲区之间的时间差。
如果您从未操作过sampleBuffers,您应该看一下Apple 的示例代码:RosyWriter
关于swift - 如何在录制时获取 AVCaptureMovieFileOutput 的当前长度以显示在屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443736/