没有合适的资源?快使用搜索试试~ 我知道了~
本文实例为大家分享了AVCaptureSession实现视频录制功能的具体代码,供大家参考,具体内容如下 #import RecordingVideoViewController.h #import <AVFoundation> #import <AssetsLibrary> @interface RecordingVideoViewController ()<AVCaptureFileOutputRecordingDelegate> //会话 负责输入和输出设备之间的数据传递 @property (strong,nona
资源详情
资源评论
资源推荐
iOS AVCaptureSession实现视频录制功能实现视频录制功能
本文实例为大家分享了AVCaptureSession实现视频录制功能的具体代码,供大家参考,具体内容如下
#import "RecordingVideoViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface RecordingVideoViewController ()<AVCaptureFileOutputRecordingDelegate>
//会话 负责输入和输出设备之间的数据传递
@property (strong,nonatomic) AVCaptureSession *captureSession;
//设备输入 负责从AVCaptureDevice获得输入数据
@property (strong,nonatomic) AVCaptureDeviceInput *videoCaptureDeviceInput;
@property (strong,nonatomic) AVCaptureDeviceInput *audioCaptureDeviceInput;
//视频输出流
@property (strong,nonatomic) AVCaptureMovieFileOutput *captureMovieFileOutput;
//相机拍摄预览图层
@property (strong,nonatomic) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
//自定义UI控件容器
@property (strong,nonatomic) UIView *viewContainer;
//聚焦图标
@property (strong,nonatomic) UIImageView *focusCursor;
//录制时长
@property (strong,nonatomic) UILabel *timeLabel;
//切换前后摄像头
@property (strong,nonatomic) UIButton *switchCameraBtn;
//改变焦距
@property (strong,nonatomic) UIButton *scaleBtn;
//计时器
@property (strong,nonatomic) NSTimer *timer;
@end
@implementation RecordingVideoViewController {
@private
NSInteger _num;
CGFloat _kCameraScale;
}
- (UIView *)viewContainer {
if (!_viewContainer) {
_viewContainer = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIButton *takeButton = [UIButton buttonWithType:UIButtonTypeCustom];
takeButton.backgroundColor = [UIColor redColor];
[takeButton setTitle:@"start" forState:UIControlStateNormal];
[takeButton addTarget:self action:@selector(takeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
_timeLabel = [[UILabel alloc] init];
_timeLabel.textColor = [UIColor redColor];
_timeLabel.textAlignment = NSTextAlignmentCenter;
_timeLabel.font = [UIFont boldSystemFontOfSize:20];
_timeLabel.text = @"00:00";
_switchCameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_switchCameraBtn setTitle:@"switch" forState:UIControlStateNormal];
_switchCameraBtn.backgroundColor = [UIColor redColor];
[_switchCameraBtn addTarget:self action:@selector(switchCameraBtnClick)
forControlEvents:UIControlEventTouchUpInside];
_scaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_scaleBtn setTitle:@"1X" forState:UIControlStateNormal];
_scaleBtn.backgroundColor = [UIColor redColor];
[_scaleBtn addTarget:self action:@selector(scaleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_viewContainer addSubview:takeButton];
[_viewContainer addSubview:_timeLabel];
[_viewContainer addSubview:_scaleBtn];
[_viewContainer addSubview:_switchCameraBtn];
[takeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(60, 40));
make.centerX.mas_equalTo(_viewContainer);
make.bottom.mas_equalTo(_viewContainer).offset(-64);
}];
[_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(_viewContainer);
make.height.mas_equalTo(@30);
make.top.mas_equalTo(_viewContainer);
}];
[_scaleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(60, 40));
make.left.mas_equalTo(_viewContainer).offset(10);
make.top.mas_equalTo(_viewContainer);
}];
[_switchCameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(60, 40));
make.top.mas_equalTo(_viewContainer);
make.right.mas_equalTo(_viewContainer).offset(-10);
}];
_focusCursor = [[UIImageView alloc] init];
kBorder(_focusCursor, 1, [UIColor yellowColor]);
_focusCursor.alpha = 0;
[_viewContainer addSubview:self.focusCursor];
[_focusCursor mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(40, 40));
make.center.mas_equalTo(_viewContainer);
}];
}
return _viewContainer;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"视频录制";
_kCameraScale = 1.0f;
//初始化会话对象
_captureSession = [[AVCaptureSession alloc] init];
if ([_captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {
_captureSession.sessionPreset = AVCaptureSessionPreset1280x720;
}
NSError *error = nil;
//获取视频输入对象
AVCaptureDevice *videoCaptureDevice = [self cameraDeviceWithPosition:(AVCaptureDevicePositionBack)];
if (!videoCaptureDevice) {
NSLog(@"获取后置摄像头失败!");
return;
}
_videoCaptureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoCaptureDevice error:&error];
if (error) {
NSLog(@"取得视频设备输入对象时出错");
return;
}
//获取音频输入对象
AVCaptureDevice *audioCatureDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];
_audioCaptureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioCatureDevice error:&error];
if (error) {
NSLog(@"取得音频设备输入对象时出错");
return;
}
//初始化设备输出对象
_captureMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
//将设备输入添加到会话中
if ([_captureSession canAddInput:_videoCaptureDeviceInput]) {
[_captureSession addInput:_videoCaptureDeviceInput];
[_captureSession addInput:_audioCaptureDeviceInput];
//防抖功能
AVCaptureConnection *captureConnection = [_captureMovieFileOutput connectionWithMediaType:AVMediaTypeAudio];
if ([captureConnection isVideoStabilizationSupported]) {
captureConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
}
}
//将设备输出添加到会话中
if ([_captureSession canAddOutput:_captureMovieFileOutput]) {
[_captureSession addOutput:_captureMovieFileOutput];
}
//创建视频预览图层
_captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
self.viewContainer.layer.masksToBounds = YES;
_captureVideoPreviewLayer.frame = self.viewContainer.bounds;
_captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:_captureVideoPreviewLayer];
//显示自定义控件
[self.view addSubview:self.viewContainer];
//添加点按聚焦手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapScreen:)];
[self.viewContainer addGestureRecognizer:tapGesture];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.captureSession startRunning];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[self.captureSession stopRunning];
[self.timer invalidate];
self.timer = nil;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.captureVideoPreviewLayer setAffineTransform:CGAffineTransformMakeScale(1, 1)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//开始 + 暂停录制
- (void)takeButtonClick:(UIButton *)sender {
if ([self.captureMovieFileOutput isRecording]) {
[self.captureMovieFileOutput stopRecording];
[self.navigationController popViewControllerAnimated:YES];
} else {
AVCaptureConnection *captureConnection = [self.captureMovieFileOutput connectionWithMediaType:AVMediaTypeVideo];
captureConnection.videoOrientation = [self.captureVideoPreviewLayer connection].videoOrientation;
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Movie.mov"];
NSLog(@"%@",filePath);
[self.captureMovieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] recordingDelegate:self];
剩余11页未读,继续阅读
weixin_38692122
- 粉丝: 13
- 资源: 960
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- pta题库答案c语言之排序4统计工龄.zip
- pta题库答案c语言之树结构7堆中的路径.zip
- pta题库答案c语言之树结构3TreeTraversalsAgain.zip
- pta题库答案c语言之树结构2ListLeaves.zip
- pta题库答案c语言之树结构1树的同构.zip
- 基于C++实现民航飞行与地图简易管理系统可执行程序+说明+详细注释.zip
- pta题库答案c语言之复杂度1最大子列和问题.zip
- 三维装箱问题(Three-Dimensional Bin Packing Problem,3D-BPP)是一个经典的组合优化问题
- 以下是一些关于Linux线程同步的基本概念和方法.txt
- 以下是一个简化的示例,它使用pygame库来模拟烟花动画的框架.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0