IOS adding UIProgressView to AVFoundation AVCaptureMovieFileOutput

元气小坏坏 提交于 2019-12-04 18:53:22

Here is how I was able to add a UIProgressView

recording is a property of AVCaptureFileOutput which is extended by AVCaptureMovieFileOutput

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];

Then in the callback method:

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

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];
            });
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!