IOS将UIProgressView添加到AVFoundation AVCaptureMovieFileOutput

我正在使用AVCaptureMovieFileOutput来录制video,我想添加一个UIProgressView来表示在video停止录制之前还剩多less时间。

我设置了15秒的最大持续时间:

 CMTime maxDuration = CMTimeMakeWithSeconds(15, 50); [[self movieFileOutput] setMaxRecordedDuration:maxDuration]; 

我似乎无法findAVCaptureMovieFileOutput是否有一个callback,当video正在录制或开始录制。 我的问题是,如何获得录制进度的更新? 或者,如果这不是可用的东西,我怎么能告诉开始录制开始计时器?

这是我能够添加UIProgressView

recording是由AVCaptureMovieFileOutput扩展的AVCaptureMovieFileOutput属性

我有一个AVCaptureMovieFileOutputtypes的variablesmovieFileOutput,我用它来捕获数据到QuickTime影片。

 @property (nonatomic) AVCaptureMovieFileOutput *movieFileOutput; 

我添加了一个观察者的logging属性来检测logging的变化。

 [self addObserver:self forKeyPath:@"movieFileOutput.recording" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:RecordingContext]; 

然后在callback方法中:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; 

我创build了一个在后台执行的while循环,然后我确保将更新发送到主线程上的视图,如下所示:

  dispatch_async([self sessionQueue], ^{ // Background task started // While the movie is recording, update the progress bar while ([[self movieFileOutput] isRecording]) { double duration = CMTimeGetSeconds([[self movieFileOutput] recordedDuration]); double time = CMTimeGetSeconds([[self movieFileOutput] maxRecordedDuration]); CGFloat progress = (CGFloat) (duration / time); dispatch_async(dispatch_get_main_queue(), ^{ // Here I dispatch to main queue and update the progress view. [self.progressView setProgress:progress animated:YES]; }); } });