如何在使用AVFoundation进行视频捕获时更新时间标签:
Mic -> AVCaptureDeviceInput -> AVCaptureSession -> AVCaptureAudioDataOutput
AVCaptureAudioDataOutput有委托和AVAssetWritter,可以将sampleBuffers写入输出文件。在这个解决方案中,我想更新录制时间。我可以在用户点击记录/暂停/恢复/停止按钮时记录时间,但是否有官方方法可以在开始时更新一些UILabel的00:00:00时间呢?我知道我可以使用NSTimer,但是当我使用AVAudioRecorder时,我有一个属性:
var currentTime: TimeInterval
// The time, in seconds, since the beginning of the recording.
以上属性也适用于暂停/继续按钮。
现在在这个新的管道中,我没有这个属性,我想知道是否我漏掉了什么,而且我无法在任何地方找到它。此外,我注意到AVCaptureSession有一个名为masterClock的CMClock属性,但我不确定如何使用它来计算时间,从录制开始算起。
我不理解这个时钟和像这样的方法的整个概念:
CMClockGetHostTimeClock()
CMSyncConvertTime
如何在AVCaptureAudioDataOutputSampleBufferDelegate方法中基于sampleBuffer获得从录制开始的秒数?
更新
我发现了一个叫做CMTimebase的类,我认为这正是我正在寻找的。目前我不知道如何使用它,但我在playground中进行实验后发现了一些东西。
import CoreMedia
let masterClock = CMClockGetHostTimeClock() // This masterclock should be from captureSession
var timebase: CMTimebase? = nil
CMTimebaseCreateWithMasterClock(kCFAllocatorDefault, masterClock, &timebase)
print(CMTimeGetSeconds(CMTimebaseGetTime(timebase!)))
CMTimebaseSetRate(timebase!, 1.0) // Start/Resume recording
sleep(1)
print(CMTimeGetSeconds(CMTimebaseGetTime(timebase!)))
sleep(1)
CMTimebaseSetRate(timebase!, 0.0) // Pause Recording
print(CMTimeGetSeconds(CMTimebaseGetTime(timebase!)))
sleep(1)
print(CMTimeGetSeconds(CMTimebaseGetTime(timebase!)))
这个对象非常有用。当我将速率设置为1.0时,它会与masterClock同步测量时间。当我将速率设置为0.0时,它停止测量时间。我还注意到我可以将Timer附加到这个对象上。不幸的是,关于Core Media的东西没有好的教程,只有一些苹果网站上的api文档。有人知道是否有关于CoreMedia Framework的WWDC相关主题的好教程/视频吗?