CLFullViewController中设置可以旋转和旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
全屏播放按钮点击事件
/** 全屏按钮点击事件 */
- (IBAction)fullViewBtnClick:(UIButton *)sender {
sender.selected = !sender.selected;
[self videoplayViewSwitchOrientation:sender.selected];
}
/** 弹出全屏播放器 */
- (void)videoplayViewSwitchOrientation:(BOOL)isFull
{
if (isFull) {
[self.contrainerViewController presentViewController:self.fullVc animated:NO completion:^{
[self.fullVc.view addSubview:self];
self.center = self.fullVc.view.center;
[UIView animateWithDuration:0.15 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
self.frame = self.fullVc.view.bounds;
} completion:nil];
}];
} else {
[self.fullVc dismissViewControllerAnimated:NO completion:^{
[self.contrainerViewController.view addSubview:self];
[UIView animateWithDuration:0.15 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
self.frame = CGRectMake(0, 200, self.contrainerViewController.view.bounds.size.width, self.contrainerViewController.view.bounds.size.width * 9 / 16);
} completion:nil];
}];
}
}
注意:这里需要拿到外面控制器来Moda出全屏播放控制器,所以给CLAVPlayerView添加contrainerViewController属性来拿到控制器。
简单封装
此时已经实现了播放器基本的功能,接下来考虑如何封装能使我们使用起来更加方便,其实我们已经将大部分封装完成,接下来需要做的就是提供简单易用的接口,使外部可以轻松调用实现播放器。
1、提供类方法快速创建播放器
+ (instancetype)videoPlayView
{
return [[[NSBundle mainBundle]loadNibNamed:@"CLAVPlayerView" owner:nil options:nil]lastObject];
}
2、播放视频的资源应该由外部决定,因此我们提供urlString属性用来接收视频的资源,然后通过重写其set方法来播放视频
/** 需要播放的视频资源set方法 */
-(void)setUrlString:(NSString *)urlString
{
_urlString = urlString;
NSURL *url = [NSURL URLWithString:urlString];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
}
此时我们在外部使用播放器就非常简单了,无需考虑内部逻辑,只需快速创建CLAVPlayerView,添加到控制器View,设置其frame,然后指定其播放视频资源就可以了。
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpVideoPlayView];
self.playView.urlString = @"http://www.easck.com/resources/videos/minion_02.mp4";
}
-(void)setUpVideoPlayView
{
self.playView = [CLAVPlayerView videoPlayView];
self.playView.frame = CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.width * 9 / 16);
self.playView.contrainerViewController = self;
[self.view addSubview:self.playView];
}










