IOS将UIProgressView添加到AVFoundation AVCaptureMovieFileOutput [英] IOS adding UIProgressView to AVFoundation AVCaptureMovieFileOutput

查看:159
本文介绍了IOS将UIProgressView添加到AVFoundation AVCaptureMovieFileOutput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AVCaptureMovieFileOutput 来录制视频,我想添加 UIProgressView 来表示有多少时间在视频停止录制之前离开。

I am using AVCaptureMovieFileOutput to record videos and I want to add a UIProgressView to represent how much time there is left before the video stops recording.

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

I set a max duration of 15 seconds:

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

我似乎无法找到 AVCaptureMovieFileOutput 有视频录制时或录制开始时的回调。我的问题是,我怎样才能获得录音进展的最新消息?或者,如果这不是可用的东西,我如何判断录制何时开始以启动计时器?

I can't seem to find if AVCaptureMovieFileOutput has a callback for when the video is recording or for when recording begins. My question is, how can I get updates on the progress of the recording? Or if this isn't something that is available, how can I tell when recording begins in order to start a timer?

推荐答案

以下是我如何添加 UIProgressView

录制 AVCaptureFileOutput 的属性,其扩展为 AVCaptureMovieFileOutput

recording is a property of AVCaptureFileOutput which is extended by AVCaptureMovieFileOutput

我有一个类型为 AVCaptureMovieFileOutput 的变量movieFileOutput,用于将数据捕获到QuickTime影片。

I have a variable movieFileOutput of type AVCaptureMovieFileOutput that I am using to capture data to a QuickTime movie.

@property (nonatomic) AVCaptureMovieFileOutput *movieFileOutput;

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

I added an observer to the recording property to detect a change in recording.

[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循环,然后我确保将更新发送到在主线程上查看如下:

I created a while loop to be executed in the background, then I made sure to dispatch updates to the view on the main thread like this:

    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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆