用系统自带的视听媒体的框架,AVFoundation录制视频文件,AVPlayer播放视频。相比UIKit框架(UIImagePickerController高度封装),AVFoundation框架让开发者有更大的发挥空间。
首先看一下效果图:
下面贴上核心控制器代码:
#import "HWVideoVC.h"
#import <AVFoundation/AVFoundation.h>
#import "HWVideoProgress.h"
@interface HWVideoVC ()<AVCaptureFileOutputRecordingDelegate>
@property (nonatomic, strong) NSTimer *timer;//定时器
@property (nonatomic, strong) AVCaptureSession *captureSession;//负责输入和输出设置之间的数据传递
@property (nonatomic, strong) AVCaptureDeviceInput *captureDeviceInput;//负责从AVCaptureDevice获得输入数据
@property (nonatomic, strong) AVCaptureMovieFileOutput *captureMovieFileOutput;//视频输出流
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;//相机拍摄预览图层
@property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskIdentifier;//后台任务标识
@property (nonatomic, assign) AVPlayer *player;//播放器对象
@property (nonatomic, weak) UIView *container;//播放器容器
@property (nonatomic, weak) UIButton *playBtn;//播放/暂停按钮
@property (nonatomic, weak) HWVideoProgress *progress;//播放进度
@property (nonatomic, weak) UILabel *currentLabel;//预览视图当前时长
@property (nonatomic, weak) UILabel *totalLabel;//预览视图总时长
@property (nonatomic, weak) UIView *previewView;//预览视图
@property (nonatomic, weak) UIView *containerView;//录制视频容器
@property (nonatomic, weak) UIImageView *focusCursor;//聚焦按钮
@property (nonatomic, weak) UIButton *finishBtn;//完成按钮
@property (nonatomic, weak) UIButton *cameraSwitchBtn;//摄像头切换按钮
@property (nonatomic, weak) UILabel *recLabel;//REC标签
@property (nonatomic, weak) UILabel *recordTimeLabel;//录制视频时长标签
@property (nonatomic, copy) NSString *path;//文件路径
@property (nonatomic, assign) NSInteger time;//录制时长
@end
@implementation HWVideoVC
- (AVPlayer *)player
{
if (!_player) {
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:_path]];
_player = [AVPlayer playerWithPlayerItem:playerItem];
_player.volume = 1.0f;
//创建播放器层
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = _container.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[_container.layer addSublayer:playerLayer];
[self addProgressObserver];
[self addObserverToPlayerItem:playerItem];
}
return _player;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"视频";
self.view.backgroundColor = [UIColor whiteColor];
//创建控件
[self creatControl];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//初始化信息
[self initVideoInfo];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.captureSession startRunning];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self stopVideoRecoding];
[self removeRecordTimer];
if (_player.rate == 1) {
[_player pause];
}
[self removeNotification];
[self.captureSession stopRunning];
}
- (void)creatControl
{
CGFloat btnW = 150.f;
CGFloat btnH = 40.f;
CGFloat marginY = 20.f;
CGFloat w = [UIScreen mainScreen].bounds.size.width;
CGFloat h = [UIScreen mainScreen].bounds.size.height;
//内容视图
CGFloat containerViewH = h - 64 - btnH - marginY * 3;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(10, 64 + marginY, w - 20, containerViewH)];
containerView.backgroundColor = [UIColor whiteColor];
containerView.layer.borderWidth = 1.f;
containerView.layer.borderColor = [[UIColor grayColor] CGColor];
[self.view addSubview:containerView];
_containerView = containerView;
//聚焦图片
UIImageView *focusCursor = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 75, 75)];
focusCursor.alpha = 0;
focusCursor.image = [UIImage imageNamed:@"camera_focus_red"];
[containerView addSubview:focusCursor];
_focusCursor = focusCursor;
//摄像头切换按钮