IOS 将 UIProgressView 添加到 AVFoundation AVCaptureMovieFileOutput

标签 ios objective-c avfoundation avcapturesession avcapturemoviefileoutput

我正在使用 AVCaptureMovieFileOutput录制视频,我想添加一个 UIProgressView表示距离视频停止录制还有多长时间。

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

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

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

最佳答案

这是我如何添加 UIProgressViewrecordingAVCaptureFileOutput 的属性由 AVCaptureMovieFileOutput 扩展

我有一个 AVCaptureMovieFileOutput 类型的变量 movieFileOutput我用来将数据捕获到 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)到主线程上的 View ,如下所示:
    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];
            });
        }
    });

关于IOS 将 UIProgressView 添加到 AVFoundation AVCaptureMovieFileOutput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26501299/

相关文章:

IOS/ Storyboard : Create Outlet Property for Text of Tabbar Item on Toolbar

objective-c - autorelease 是否隐藏了 Instruments 中的泄漏?

objective-c - NSKeyedArchiver:如何归档包含自定义结构的 NSValue

ios - 在 ScrollView 内滚动 TableView

objective-c - 使用Cocoa Drawing鼠标点击对象的回调函数?

ios - 如何在 AppRTC iOS 示例中将音频重定向到扬声器?

ios - 如何在 Objective-C 中返回一个函数

ios - 如何使用#selector 函数传递变量?以及将什么传递给 textField : UITextField?

ios - 如何在运行 session 期间更改 AVCaptureMovieFileOutput 视频方向?

ios - 从 VTCompressionOutputCallback 中引用 `self`