1

我使用AVCaptureMovieFileOutput来录制视频,我想添加一个UIProgressView来表示在视频停止录制之前还剩多少时间。IOS将UIProgressView添加到AVFoundation AVCaptureMovieFileOutput

我设置为15秒,最长持续时间是:

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

我似乎无法找到,如果AVCaptureMovieFileOutput有当视频录制或录制开始时回调。我的问题是,如何获得录制进度的更新?或者,如果这不是可用的,我怎么能告诉开始录制以启动定时器?

回答

3

这里是我如何能够增加一个UIProgressView

recording是由AVCaptureMovieFileOutput

AVCaptureMovieFileOutput类型的变量movieFileOutput,我使用将数据捕获到一个扩展的AVCaptureFileOutput属性QuickTime电影。

@property (nonatomic) AVCaptureMovieFileOutput *movieFileOutput; 

我在记录属性中添加了一个观察者来检测记录的变化。

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

然后在回调方法:

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

我创建了一个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]; 
      }); 
     } 
    }); 
+0

是否正在更新无限循环中的UI?这一定非常低效。 – Vojto 2017-09-28 19:58:43