IOS开发之两种方式初始化视频播放器
介绍两种播放视频的方法 第一种稍微复杂一点。
第一种方式播放视频:
一、视频播放基础知识
1.介绍
ios9.0的改变 弃用了MPMoviePlayerViewController 导入MediaPlayer框架
现使用AVPlayerViewController 导入AVFoundation框架
(1)AVPlayer 播放音视频 视频操作相关
《1》play
《2》pause
《3》seekToTime:跳转进度
《4》currentItem 当前播放的视频元素
《5》volume 调节音量
《6》externalPlaybackVideoGravity 视频显示的播放样式
AVLayerVideoGravityResizeAspect 普通的
AVLayerVideoGravityResizeAspectFill 充满的
《7》currentTime 获得当前播放时间 -> CMTime
CMTimeGetSeconds(<#CMTime time#>) 通过CMTime获得当前播放时间(秒)
(2)AVPlayerItem 音视频对象 视频内容相关
《1》duration 总时长
《2》status 加载的状态
①AVPlayerItemStatusUnknown 未知的状态
②AVPlayerItemStatusReadyToPlay 准备播放状态
③AVPlayerItemStatusFailed 失败状态
《3》时间控制的一个类目
① currentTime 获得当前播放时间
② forwardPlaybackEndTime跳到结束位置
③ reversePlaybackEndTime 调到开始位置
④ seekToTime: 调到指定位置
(3)AVPlayerLayer播放显示视频的图层界面
(4)AVPlayerViewController 视图控制器 #import <AVKit/AVKit.h> 可以显示视频 并且有调节控件
2.使用
(1)AVPlayer直接播放
《1》创建AVPlayerItem
<1>创建方式
①playerItemWithURL:类方法通过URl地址创建要播放的对象 可以是本地内容也可以是在线内容
②initWithURL:构造方法
③ playerItemWithAsset:通过设备相册里面的内容 创建一个 要播放的对象
④playerItemWithAsset:automaticallyLoadedAssetKeys自动根据要求的Key去加载相册里面的内容
⑤initWithAsset:automaticallyLoadedAssetKeys:
《2》创建AVPlayer
<1>创建方式
①playerWithURL:根据URL去创建播放器 不需使用item
②initWithURL:
③playerWithPlayerItem:需要传入一个视频播放内容
④initWithPlayerItem:
⑤获得播放结束的状态
⑥ seekToTime:跳转到某一个进度
⑦CMTime:结构体 value(进度)timeScale(帧率)
《3》创建AVPlayerLayer 添加到父视图
<1>根据播放器去创建一个可以显示视频的图层playerLayerWithPlayer:->类方法
<2>设置位置frame
<3>把视频图层添加到父图层
《4》播放****** [播放器 play];
实例代码:
导入框架:#import <AVFoundation/AVFoundation.h>
声明全局变量:
@interface ViewController ()
{
AVPlayer *player;
}
@end
在ViewDidLoad中
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"TICK" forState:UIControlStateNormal];
button.backgroundColor = [UIColor brownColor];
[button addTarget:self action:@selector(doit:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
按钮的触发方法:
- (void)doit:(UIButton *)sender{
/*
视频播放需要 AVPlayer AVPlayerItem AVPlayerLayer
AVPlayer(视频播放器)去播放->AVPlayerItem(视频播放的元素)->AVPlayerLayer展示播放的视图
*/
// 1.创建要播放的元素
NSURL *url = [[NSBundle mainBundle]URLForResource:@"IMG_9638.m4v" withExtension:nil];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
// 2.创建播放器
player = [AVPlayer playerWithPlayerItem:item];
// 3.创建视频显示的图层
AVPlayerLayer *showVodioLayer = [AVPlayerLayer playerLayerWithPlayer:player];
showVodioLayer.frame = self.view.frame;
[self.view.layer addSublayer:showVodioLayer];
// 4.播放视频
[player play];
// 获得播放结束的状态 -> 通过发送通知的形式 获得 -> AVPlayerItemDidPlayToEndTimeNotification
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(itemDidPlayToEndTime:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
// CMTime 结构体 表示视频播放的进度的 value(进度) timescale(帧率)
// CMTimeMake(<#int64_t value#>, <#int32_t timescale#>)
// kCMTimeZero 表示初始进度
// seekToTime:<#(CMTime)#> 跳转到某一个进度
// player.currentItem.status 只要可以得到当前视频元素准备好的状态 就可以得到总时长了
// 采取KVO的形式获得 视频总时长 通过监视status 来判断是否准备好的状态
[item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
// 快进
// 跳到某一个进度的方法 -> seekToTime:
// 得到当前的时间 + 快进的时间
}
滑动屏幕让视频快进
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 获得到当前播放的时间(秒)
Float64 cur = CMTimeGetSeconds(player.currentTime);
cur += 2;
[player seekToTime:CMTimeMake(cur, 1)];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"%@",change[@"new"]);
AVPlayerItemStatus status = [change[@"new"] integerValue];
switch (status) {
case 0:{
NSLog(@"未知");
break;
}
case 1:{
NSLog(@"总时长:%f",CMTimeGetSeconds(player.currentItem.duration));
break;
}
case 2:{
NSLog(@"失败");
break;
}
default:
break;
}
}
- (void)itemDidPlayToEndTime:(NSNotification *)not{
NSLog(@"播放结束");
[player seekToTime:kCMTimeZero]
}
第二种方式比较简单:
使用
AVPlayerViewController 视图控制器可以显示视频 并且有调节控件
注意导入框架:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"TICK" forState:UIControlStateNormal];
button.backgroundColor = [UIColor brownColor];
[button addTarget:self action:@selector(playMovie) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
按钮的触发事件:
- (void)playMovie{
// 1.AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:[[NSBundle mainBundle] URLForResource:@"IMG_9638.m4v" withExtension:nil]];
// 2.创建视频播放视图的控制器
AVPlayerViewController *playerVC = [[AVPlayerViewController alloc]init];
playerVC.player = player;
// 隐藏 控制控件
// playerVC.showsPlaybackControls = NO;
// [self presentViewController:playerVC animated:YES completion:nil];
// 自定义位置
playerVC.view.frame = CGRectMake(200, 300, 300, 300);
[self.view addSubview:playerVC.view];
[self addChildViewController:playerVC];
}