自定义播放器:
1.初始化播放器:(1)根据网址进行编码 (2)对网址进行编码 (3)根据网址,创建视频项目对象 playerItem(4)创建视频播放器对象player(5)创建一个播放器承载对象,把播放器放到上面playerLayer(5)设置一下承载对象的尺寸(6)设置视频的填充方式(7)把播放器添加到layer层上
// 对网址进行编码
urlStr =[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlStr];
#pragma mark 1.初始化播放器
- (void)createPlayer{
NSString *urlStr = [NSString stringWithFormat:@"http://flv2.bn.netease.com/videolib3/1505/24/HYUCE6348/SD/HYUCE6348-mobile.mp4"];
// 对网址进行编码
urlStr =[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlStr];
// 根据网址,创建视频项目对象
self.playerItem = [[AVPlayerItem alloc] initWithURL:url];
// 创建视频播放器对象
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
// 创建一个播放器承载对象,把播放器放到上面
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
// 设置一下承载对象的尺寸
self.playerLayer.frame = CGRectMake(5, 5, SCREEN_WIDTH - 10, 290);
// 设置视频的填充方式 (填满)
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// 把播放器添加到layer层上
[self.playerView.layer insertSublayer:self.playerLayer above:0];
//
self.playerView.backgroundColor = [UIColor whiteColor];
}
2.播放视频
[self.player play];
3.给当前屏幕添加一个监听横竖屏的视频结束的方法
- (void)addNotificationCenters{
// 3.1 监听当前的视频播放情况,只要播放结束,就触发指定方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
// 3.2 监听当前屏幕的旋转
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
3.1设置横竖屏尺寸
#pragma mark 3.2 - 1 设置横屏尺寸
- (void)setPlayerLayerFrame{
self.playerLayer.frame = CGRectMake(5, 5, SCREEN_WIDTH - 10, 290);
}
#pragma mark 3.2 - 2 设置竖屏的尺寸
- (void)setPortraitFrame{
self.playerLayer.frame = CGRectMake(5, 5, SCREEN_WIDTH - 10, 290);
}
3.2监听屏幕旋转
#pragma mark 3.2 监听屏幕旋转
- (void)screenChange:(NSNotification *)nitification{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
NSLog(@"%ld", orientation);
// 根据横竖屏,分别设置播放器的frame
if (orientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"向左横屏");
[self setPlayerLayerFrame];
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"向右横屏");
[self setPlayerLayerFrame];
} else if (orientation == UIInterfaceOrientationPortrait) {
NSLog(@"竖屏");
[self setPortraitFrame];
}
}
4.创建一个轻点的手势(点击隐藏按钮栏,用view动画改变透明度)
#pragma mark 4 创建轻点的手势
- (void)createTap{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.view addGestureRecognizer:tap];
self.isTap = NO;
}
#pragma mark 4.1 轻点之后实现视图的显示和隐藏(用view动画实现)
- (void)tapAction:(UITapGestureRecognizer *)tap{
if (self.isTap == NO) {
[UIView animateWithDuration:0.5 animations:^{
self.bottomView.alpha = 0;
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
self.bottomView.alpha = 0.5;
}];
}
self.isTap = !self.isTap;
}
5.对播放器的进度进行设置
// CMTime是专门用来表示视频时间的
// CMTimeMake是用来创建CMTime的
// 用法就是CMTimeMake(time, timeScale)
// time指的是时间,但不是秒,如果想转换成秒需要通过第二个参数来完成
// timeScale指的是1秒需要多少帧构成,而真正的时间是 time/timeScale
#pragma mark 5.对播放进度的设置
- (void)addProgress{
// 设置成(1,1) 每秒执行一次
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
// 当前视频的总时间,将CMTime转换成CGFloat
CGFloat durationTime = CMTimeGetSeconds(self.playerItem.duration);
// 当前时间
CGFloat currentTime = CMTimeGetSeconds(self.playerItem.currentTime);
// 倒计时
CGFloat rem = durationTime - currentTime;
// 把时间转换成NSString,在进行赋值
NSString *tltaleT = [NSString stringWithFormat:@"%02d:%02d", (int)rem / 60, (int)rem % 60];
NSString *currentT = [NSString stringWithFormat:@"%02d:%02d", (int)currentTime / 60, (int)currentTime % 60];
NSString *timeStr = [NSString stringWithFormat:@"%@/%@", tltaleT, currentT];
// 给lable赋值
self.timeLabel.text = timeStr;
// 让slider也向前移动
self.progressSlider.value = currentTime / durationTime;
// 保存总时长,用于手动快进的时候
self.duration = durationTime;
}];
}