iOS-视频录制库、
2015-05-06 本文已影响519人
Stark_Dylan
既然有了播放在线视频, 那么我们也把录制加进来吧。
首先初始化我们的视频录制器, 并添加到View
self.captureManager = [[AVCaptureManager alloc] initWithPreviewView:self.view];
self.captureManager.delegate = self;
设置代理并写代理方法
#pragma mark - AVCaptureManagerDeleagte
- (void)didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL error:(NSError *)error {
if (error) {
NSLog(@"error:%@", error);
return;
}
if (!isNeededToSave) {
return;
}
[self saveRecordedFile:outputFileURL];
}
开始录制 结束录制
[self.captureManager startRecording];
[self.captureManager stopRecording];
保存录制后的视频 - 写到代理方法里的
- (void)saveRecordedFile:(NSURL *)recordedFile {
[SVProgressHUD showWithStatus:@"Saving..."
maskType:SVProgressHUDMaskTypeGradient];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary writeVideoAtPathToSavedPhotosAlbum:recordedFile
completionBlock:
^(NSURL *assetURL, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
NSString *title;
NSString *message;
if (error != nil) {
title = @"Failed to save video";
message = [error localizedDescription];
}
else {
title = @"Saved!";
message = nil;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
});
}];
});
}
.h文件
@property (nonatomic, assign) id<AVCaptureManagerDelegate> delegate;
@property (nonatomic, readonly) BOOL isRecording;
- (id)initWithPreviewView:(UIView *)previewView;
- (void)toggleContentsGravity;
- (void)resetFormat;
- (void)switchFormatWithDesiredFPS:(CGFloat)desiredFPS;
- (void)startRecording;
- (void)stopRecording;