【问题标题】:How to get current length of AVCaptureMovieFileOutput while recording to display on screen?如何在录制以显示在屏幕上时获取 AVCaptureMovieFileOutput 的当前长度?
【发布时间】:2016-07-18 18:33:02
【问题描述】:

正如标题所说,我希望在用户使用 AVFoundation 录制视频时显示录制的当前长度。我用 AVAudioRecorder 做了类似的事情,我创建了一个计时器来设置标签的文本,如下所示:

recordDurationLabel.text = timeString((audioRecorder?.currentTime)!)

我想知道是否有用于录制视频的“currentTime”功能?我唯一的另一个想法是在 captureOutput:didStartRecordingToOutputFileAtURL 函数中启动一个重复的 1 秒计时器,但我想确保准确性。

提前致谢!

编辑: 现在,这是我在 captureOutput:didStartRecordingToOutputFileAtURL 委托方法中实现的功能,但同样,与视频直接相关的内容会更清晰。

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
}

【问题讨论】:

    标签: swift video time avfoundation recording


    【解决方案1】:

    使用AVCaptureMovieFileOutput,您可以使用只读的recordedDuration 获取电影的持续时间,并在录制开始时返回一个CMTime。见AVCaptureFileOutput

    如果您使用captureOutput:didOutputSampleBuffer:fromConnection:,您将收到包含CMTimeCMSampleBufferobjects,您可以使用CMSampleBufferGetPresentationTimeStamp 访问它。

    CMTimeSubstract 这样的CMTime 操作将允许您计算当前缓冲区与您记录的第一个缓冲区之间的时间差。

    如果你从未操作过 sampleBuffers,你应该看看 Apple 的这个示例代码:RosyWriter

    【讨论】:

    • 谢谢,我希望避免使用 AVCaptureVideoDataOutput,但似乎这将是这样做的方法。
    • 糟糕,我很少使用 MovieFileOutput。我检查了文档,只需保留对您的输出的引用,并使用recordedDuration 访问持续时间。编辑了答案
    猜你喜欢
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2017-04-04
    • 1970-01-01
    • 2013-03-24
    相关资源
    最近更新 更多