快速使用
SCRecorder *recorder = [SCRecorder recorder]; // You can also use +[SCRecorder sharedRecorder]
if (![recorder startRunning]) {
NSLog(@"Something wrong there: %@", recorder.error);
}
recorder.session = [SCRecordSession recordSession];
[recorder record];
[recorder pause];
SCRecorder
init方法
_sessionQueue = dispatch_queue_create("me.corsin.SCRecorder.RecordSession", nil);
创建一个_sessionQueue队列,用于向SCRecordSession发送信息
dispatch_queue_set_specific方法向指定的_sessionQueue设置一个kSCRecorderRecordSessionQueueKey的标识,这两个方法可以解决GCD方法可重入问题,这里还有一个说UI线程操作的例子。
dispatch_set_target_queue(_sessionQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
dispatch_set_target_queue除了能用来设置队列的优先级之外,还能够创建队列的层次体系,当我们想让不同队列中的任务同步的执行时,我们可以创建一个串行队列,然后将这些队列的target指向新创建的队列即可。
在这里,把_sessionQueue的优先级设为跟dispatch_get_global_queue一样的优先级。
事件通知方法
一系列在init时注册的通知方法
屏幕旋转方法
//都是私有方法
- (void)updateVideoOrientation;
- (AVCaptureVideoOrientation)actualVideoOrientation;
准备session和配置输入输出
关于输入输出配置的问题看另一篇文章
1 - (BOOL)prepare:(NSError **)error
2 - (void)beginConfiguration
3 - (void)commitConfiguration
4 - (BOOL)_reconfigureSession
5 - (void)reconfigureVideoInput:(BOOL)shouldConfigureVideo audioInput:(BOOL)shouldConfigureAudio
6 - (void)configureDevice:(AVCaptureDevice*)newDevice mediaType:(NSString*)mediaType error:(NSError**)error
7 - (AVCaptureDeviceInput*)currentDeviceInputForMediaType:(NSString*)mediaType
- 方法1用来初始化AVCaptureSession,然后依次调用2、4、5、3
- 方法2/3不提
- 方法4:
- 设置sessionPreset
- 根据fastRecordMethodEnabled属性设置
AVCaptureVideoDataOutput
,AVCaptureAudioDataOutput
,AVCaptureMovieFileOutput
,AVCaptureStillImageOutput
四种响应链终点类
- 方法5/6/7:根据
shouldConfigureVideo
和shouldConfigureAudio
两个参数确定是否重新配置AVCaptureDevice和AVCaptureDeviceInput,并重新添加到captureSession中
session开始预览视频
- (BOOL)startRunning
- (void)stopRunning
两个方法里调用AVCaptureSession的startRunning
和stopRunning
方法
录制和暂停
1 - (void)record
2 - (void)_progressTimerFired:(NSTimer *)progressTimer
3 - (void)pause
4 - (void)pause:(void(^)())completionHandler
5 - (NSDictionary *)_createSegmentInfo
无论录制还是暂停方法,都要在_sessionQueue执行相关block,所以有下段代码
if ([SCRecorder isSessionQueue]) {
block();
} else {
dispatch_sync(_sessionQueue, block);
}
方法一:经由- (void)beginRecordSegmentIfNeeded:(SCRecordSession *)recordSession
方法到SCRecordSession使movieFileOutput开始录制到指定url
方法二:不提
方法三、四:经由SCRecordSession使停止录制并把录制段落添加到_segments数组中
AVCaptureAudioDataOutputSampleBufferDelegate和AVCaptureVideoDataOutputSampleBufferDelegate处理流程
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
- (void)_handleVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer withSession:(SCRecordSession *)recordSession connection:(AVCaptureConnection *)connection
- (void)_handleAudioSampleBuffer:(CMSampleBufferRef)sampleBuffer withSession:(SCRecordSession *)recordSession
- (void)appendVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer toRecordSession:(SCRecordSession *)recordSession duration:(CMTime)duration connection:(AVCaptureConnection *)connection completion:(void(^)(BOOL success))completion
- (SCFilter *)_transformFilterUsingBufferWidth:(size_t)bufferWidth bufferHeight:(size_t)bufferHeight mirrored:(BOOL)mirrored
AVCaptureFileOutputRecordingDelegate
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error