#import <AVFoundation/AVFoundation.h>

#import <AssetsLibrary/AssetsLibrary.h>

@interface ViewController ()<AVCaptureFileOutputRecordingDelegate>

@property(nonatomic,strong)AVCaptureSession *session;

@property(nonatomic,strong)AVCaptureDevice *videoDevice;

@property(nonatomic,strong)AVCaptureDevice *audioDevice;

@property(nonatomic,strong)AVCaptureDeviceInput *videoInput;

@property(nonatomic,strong)AVCaptureDeviceInput *audioInput;

@property(nonatomic,strong)AVCaptureMovieFileOutput *movieFileOutput;

@property(nonatomic,strong)AVCaptureVideoPreviewLayer *videoLayer;

@property(nonatomic,assign)UIBackgroundTaskIdentifier backgroundTaskIdentifier;

@property (weak, nonatomic) IBOutlet UIButton *RecordButton;

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[self initWithSession];

}

- (void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

[self.session startRunning];

}

- (void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear:animated];

[self.session stopRunning];

}

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor orangeColor];

}

#pragma mark  -初始化

- (void)initWithSession{

_session = [[AVCaptureSession alloc]init];

if ([_session canSetSessionPreset:AVCaptureSessionPreset1280x720]) {

[_session setSessionPreset:AVCaptureSessionPreset1280x720];

}

NSArray *deviceArray = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

for (AVCaptureDevice *device in deviceArray) {

if (device.position == AVCaptureDevicePositionBack) {

_videoDevice = device;

}

}

_audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio]firstObject];

NSError *error = nil;

_videoInput = [[AVCaptureDeviceInput alloc]initWithDevice:_videoDevice error:&error];

_audioInput = [[AVCaptureDeviceInput alloc]initWithDevice:_audioDevice error:&error];

if ([_session canAddInput:_videoInput]) {

[_session addInput:_videoInput];

}

if ([_session canAddInput:_audioInput]) {

[_session addInput:_audioInput];

}

_movieFileOutput = [[AVCaptureMovieFileOutput alloc]init];

if ([_session canAddOutput:_movieFileOutput]) {

[_session addOutput:_movieFileOutput];

}

AVCaptureConnection *connection = [_movieFileOutput connectionWithMediaType:AVMediaTypeVideo];

//此处是为了设置视频防抖动在iOS8以后才有,需要加系统判断

if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){

if ([connection isVideoStabilizationSupported]) {

connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeCinematic;//在iOS8以后才有效,要加判断

}

}

_videoLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];

_videoLayer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 500);

self.view.layer.masksToBounds = YES;

_videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[self.view.layer addSublayer:_videoLayer];

_RecordButton.selected = NO;

}

#pragma mark  --当 拍摄 按钮点击

- (IBAction)takePhoto:(id)sender {

_RecordButton.selected = !_RecordButton.selected; //改变按钮状态切换上面文字

AVCaptureConnection *captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];

if (![self.movieFileOutput isRecording]) {

//如果支持多任务则开始多任务

if ([[UIDevice currentDevice] isMultitaskingSupported]) {

self.backgroundTaskIdentifier = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];

}

//预览层和视频方向保持一致

captureConnection.videoOrientation = [self.videoLayer connection].videoOrientation;

//建立录制缓存文件

NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingString:@"mMovie.mov"];

NSURL *fileUrl = [NSURL fileURLWithPath:outputFilePath];

//此句是为了开始录制,并设置代理

[self.movieFileOutput  startRecordingToOutputFileURL:fileUrl recordingDelegate:self];

}

else

{

[self.movieFileOutput stopRecording];

}

}

#pragma mark 视频输出代理

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections

{

NSLog(@"开始录制");

}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error

{

NSLog(@"视频录制完成");

UIBackgroundTaskIdentifier lastBackgroundTaskIdentifier = self.backgroundTaskIdentifier;

self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;

ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc]init];

[assetLibrary writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {

if (error) {

NSLog(@"保存视频到相薄发生错误");

}

if(lastBackgroundTaskIdentifier != UIBackgroundTaskInvalid)

{

[[UIApplication sharedApplication]endBackgroundTask:lastBackgroundTaskIdentifier];

}

NSLog(@"成功保存视频到相薄");

NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingString:@"mMovie.mov"];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) {

[[NSFileManager defaultManager]removeItemAtPath:outputFilePath error:nil];

}

}];

}

AVFoundation自定义录制视频的更多相关文章

  1. Android自定义view之仿微信录制视频按钮

    本文章只写了个类似微信的录制视频的按钮,效果图如下:             一.主要的功能: 1.长按显示进度条,单击事件,录制完成回调 2.最大时间和最小时间控制 3.进度条宽度,颜色设置 二.实 ...

  2. iOS 三种录制视频方式

    随着每一代 iPhone 处理能力和相机硬件配置的提高,使用它来捕获视频也变得更加有意思.它们小巧,轻便,低调,而且与专业摄像机之间的差距已经变得非常小,小到在某些情况下,iPhone 可以真正替代它 ...

  3. 根据分析查看相关知识点分析iOS 三种录制视频方式

    这篇文章讨论了关于如何配置视频捕获管线 (pipeline) 和最大限度地利用硬件性能的一些不同选择. 这里有个使用了不同管线的样例 app,可以在 GitHub 查看. 第一种:UIImagePic ...

  4. iOS录制视频

    随着每一代 iPhone 处理能力和相机硬件配置的提高,使用它来捕获视频也变得更加有意思.它们小巧,轻便,低调,而且与专业摄像机之间的差距已经变得非常小,小到在某些情况下,iPhone 可以真正替代它 ...

  5. iOS AVCaptureVideoDataOutputSampleBufferDelegate 录制视频

    iOS AVCaptureVideoDataOutputSampleBufferDelegate 录制视频 应用场景: 使用AVFoundation提供的API, 我们可以从 AVCaptureVid ...

  6. 3D图片采集与展示(SurfaceView 自适应 Camera, 录制视频, 抽取帧)

    最近在做一个3D图片采集与展示. 主要功能为:自定义Camera(google 已经摈弃了Camera, 推荐使用Camera2,后续篇幅,我将会用Camera2取代Camera),围绕一个物体360 ...

  7. iOS 录制视频MOV格式转MP4

    使用UIImagePickerController系统控制器录制视频时,默认生成的格式是MOV,如果要转成MP4格式的,我们需要使用AVAssetExportSession; 支持转换的视频质量:低, ...

  8. bandicam如何录制视频

    我们一般都很熟悉这类软件:屏幕录制专家和kk录制等,这些都是国内比较优秀的作品.不过exe的封装格式以及录制的清晰度让人很纠结.所以这里要为大家分享的是一款韩国人写录制软件Bandicam.Bandi ...

  9. Android手机录制视频 实时传输(转载)

    最近调研android视频录制.另一部手机实时观看,大致有以下几种思路. 1. android手机充当服务器,使用NanoHTTPD充当服务器,另一部手机或者pc通过输入http://手机的ip:80 ...

随机推荐

  1. LINQ to Sql系列二 简单查询和联接查询

    这一篇文章主要总结LINQ to sql的简单查询(单表查询)和联接查询(多表查询) 单表查询 需求是我们要输出TClass表中的结果.使用了from-in-select语句,代码如下: public ...

  2. WPF中将16进制颜色码转换成SolidColorBrush

    使用ColorConverter.ConvertFromString(string colorValue)方法 例如:new SolidColorBrush((Color)ColorConverter ...

  3. html标签大全(2)

    http标签详解 声明 1:这里的文字都是我从我自己csdn账号拷贝过来,是本人学习总结的结晶,所以请尊重本作品.2:如要要转载本文章,则要说明文字的出处.3:如有哪里不对欢迎指出. 在上一篇文章中主 ...

  4. Bootstrap入门(十四)组件8:媒体对象

    Bootstrap入门(十四)组件8:媒体对象 这是一个抽象的样式,用以构建不同类型的组件,这些组件都具有在文本内容的左或右侧对齐的图片(就像博客评论或 Twitter 消息等). 1.基本样式 2. ...

  5. saiku的源码包Bulid常见问题和jar包

    最近在做kylin+mondrian+saiku的二次开发的时候,Bulid saiku的源码出现了很多问题,基本上一大部分问题jar找不到问题,很多jar国内网站都找不到.这时候只有手动下载然后注册 ...

  6. SQL Server-聚焦SNAPSHOT基于行版本隔离级别详解(三十)

    前言 上一篇SQL Server详细讲解了隔离级别,但是对基于行版本中的SNAPSHOT隔离级别仍未完全理解,本节再详细讲解下,若有疑义或不同见解请在评论中提出,一起探讨. SNAPSHOT行版本隔离 ...

  7. C# 6 与 .NET Core 1.0 高级编程 - 37 章 ADO.NET

    译文,个人原创,转载请注明出处,有不对的地方欢迎指出与交流. 英文原文:Professional C# 6 and .NET Core 1.0 - 37 ADO.NET --------------- ...

  8. executssql 函数的每一句代码的意思

    Public Function Executesql(ByVal sql As String, Msgstring As String) As ADODB.Recordset Dim cnn As A ...

  9. Android jni 编程2(对基本类型一维整型数组的操作)

    参考教程和这位博主的对一维数组的处理,主要包括以下三种类型: //传入一维数组,无返回值 public native void arrayEncode(int[] arr); //传一个一维数组和数组 ...

  10. [转]CentOS 6.3下Samba服务器的安装与配置

    一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microsoft的 ...