1

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

我将最大持续时间设置为 15 秒:

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

我似乎找不到AVCaptureMovieFileOutput视频录制时间或录制开始时间是否有回调。我的问题是,如何获取录制进度的最新信息?或者,如果这不是可用的东西,我如何知道何时开始录制以启动计时器?

4

1 回答 1

3

这是我如何能够添加一个UIProgressView

recordingAVCaptureFileOutput其扩展的属性AVCaptureMovieFileOutput

我有一个类型的变量 movieFileOutput,AVCaptureMovieFileOutput用于将数据捕获到 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];
            });
        }
    });
于 2014-10-23T02:51:23.140 回答