个人笔记 AVfoundation框架的简单实用 自定义录制视频界面
2014-12-12 14:33
489 查看
一、创建拍摄界面
首先需要声明4个变量
1.
录制视频是基于会话的 声明一个会话作为 所以动作的载体
self.session=[[AVCaptureSessionalloc]init];
//设置画面质量
[self.sessionsetSessionPreset:AVCaptureSessionPresetLow];
2.声明一个主体设备 说明这个设备是拍摄视频的
self.captureDevice=[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
3.声明一个输入设备 这个设备是基于 主体设备的
self.inputDevice=[AVCaptureDeviceInputdeviceInputWithDevice:self.captureDeviceerror:nil];
//判断会话可否加入设备
if ([self.sessioncanAddInput:self.inputDevice]
)
{
[self.sessionaddInput:self.inputDevice];
}
4.这一段是设置拍摄界面的frame 的
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayeralloc]
initWithSession:self.session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[selfview]
layer];
[rootLayersetMasksToBounds:YES];
[previewLayersetFrame:CGRectMake(-70,0,
rootLayer.bounds.size.height, rootLayer.bounds.size.height)];
[rootLayerinsertSublayer:previewLayer
atIndex:0];
5.声明一个输出设备
self.movieFileOutput=[[AVCaptureMovieFileOutputalloc]init];
6.将输出设备添加到会话中
[self.sessionaddOutput:self.movieFileOutput];
7.启动设备
[self.sessionstartRunning];
这样设备就运行了 但此时 还没有进行拍摄
要进行拍摄 需要引入 代理
AVCaptureFileOutputRecordingDelegate
二、然后在你拍摄按钮 加入点击方法
- (void) startRecording
{
NSString *filePath = [BMDAOgetVideoSaveFilePathString];//这个是设置
拍摄完毕时候视频的存储位置
[self.movieFileOutputstartRecordingToOutputFileURL:[NSURLfileURLWithPath:filePath]
recordingDelegate:self];
}
代码中加入代理方法
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL
*)fileURL fromConnections:(NSArray *)connections
{
NSLog(@"start record video");
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL
*)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{
ALAssetsLibrary *library = [[ALAssetsLibraryalloc]
init];
//将临时文件夹中的视频文件复制到
照片文件夹中,以便存取
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
completionBlock:^(NSURL *assetURL,NSError
*error) {
if (error) {
//
}
下一篇说闪光灯和切换摄像头
else
[WaitVCshowWaitViewWithMessage:[assetURL
path]];
}];
}
首先需要声明4个变量
1.
录制视频是基于会话的 声明一个会话作为 所以动作的载体
self.session=[[AVCaptureSessionalloc]init];
//设置画面质量
[self.sessionsetSessionPreset:AVCaptureSessionPresetLow];
2.声明一个主体设备 说明这个设备是拍摄视频的
self.captureDevice=[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
3.声明一个输入设备 这个设备是基于 主体设备的
self.inputDevice=[AVCaptureDeviceInputdeviceInputWithDevice:self.captureDeviceerror:nil];
//判断会话可否加入设备
if ([self.sessioncanAddInput:self.inputDevice]
)
{
[self.sessionaddInput:self.inputDevice];
}
4.这一段是设置拍摄界面的frame 的
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayeralloc]
initWithSession:self.session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[selfview]
layer];
[rootLayersetMasksToBounds:YES];
[previewLayersetFrame:CGRectMake(-70,0,
rootLayer.bounds.size.height, rootLayer.bounds.size.height)];
[rootLayerinsertSublayer:previewLayer
atIndex:0];
5.声明一个输出设备
self.movieFileOutput=[[AVCaptureMovieFileOutputalloc]init];
6.将输出设备添加到会话中
[self.sessionaddOutput:self.movieFileOutput];
7.启动设备
[self.sessionstartRunning];
这样设备就运行了 但此时 还没有进行拍摄
要进行拍摄 需要引入 代理
AVCaptureFileOutputRecordingDelegate
二、然后在你拍摄按钮 加入点击方法
- (void) startRecording
{
NSString *filePath = [BMDAOgetVideoSaveFilePathString];//这个是设置
拍摄完毕时候视频的存储位置
[self.movieFileOutputstartRecordingToOutputFileURL:[NSURLfileURLWithPath:filePath]
recordingDelegate:self];
}
代码中加入代理方法
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL
*)fileURL fromConnections:(NSArray *)connections
{
NSLog(@"start record video");
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL
*)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{
ALAssetsLibrary *library = [[ALAssetsLibraryalloc]
init];
//将临时文件夹中的视频文件复制到
照片文件夹中,以便存取
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
completionBlock:^(NSURL *assetURL,NSError
*error) {
if (error) {
//
}
下一篇说闪光灯和切换摄像头
else
[WaitVCshowWaitViewWithMessage:[assetURL
path]];
}];
}
相关文章推荐
- 【个人收藏】Direct UI 与一个简单的开源DIRECTUI界面框架
- 自定义相机及视频录制界面
- iOS开发进阶 - 用AVFoundation自定义视频录制功能
- iOS开发进阶 - 用AVFoundation自定义视频录制功能
- otto框架简单的使用(个人笔记)
- 个人笔记(孔浩Maven视频教程)——03.在Eclipse中建立简单的项目
- 简单实用后台任务执行框架(Struts2+Spring+AJAX前端web界面可以获取进度)
- iOS自定义视频录制界面
- iOS开发进阶 - 用AVFoundation自定义视频录制功能
- iOS 视频录制、播放,使用视听媒体框架AVFoundation实现视频录制播放 —— HERO博客
- ios开发技巧之:用AVFoundation自定义视频录制功能
- 归纳笔记022:AVFoundation拍照和录制视频
- Android学习个人笔记1-Layout界面布局-xml,java混合模式-自定义view
- Directshow学习笔记五-----一个简单的视频播放程序(个人学习总结,仅供参考)
- opencv 视频处理框架,面向对象,简单实用,可扩展
- 一个简单实用的数据库操作框架
- 根据红头发组播视频,整理的个人组播笔记(一)
- 自定义iPod界面“隐藏”iPhone视频
- 测试自动化框架之----自定义报表与错误处理两个模块视频
- 自定义Gridview用户控件,简单实用,希望大家多提宝贵意见