美文网首页
iOSWebView播放视频进行屏幕旋转

iOSWebView播放视频进行屏幕旋转

作者: SinceMeYou | 来源:发表于2020-03-01 00:06 被阅读0次

前言: 项目中要求H5页面播放的视频,点击播放要进行自动横屏播放,并且支持自动旋转,故引出了XXTWebVideoRotate这个第三方库

介绍:此第三方库为了方便调用,故采用的是UIViewController的分类进行处理,若项目中多个位置需要进行网页视频播放的监听(一般不存在的啊),两行代码即可使用,接下来介绍下如何实现的。

整体思路:
(1)、首先监听网页中点击了视频播放按钮,UIWindowDidBecomeVisibleNotification 其实是监听window的变化的,此处就是利用这个监听处理。
(2)、在(1)中的监听方法中添加设备方向的监听和进行屏幕旋转的处理
(3)、在屏幕旋转的监听中会根据屏幕旋转的方向进行旋转操作
(4)、在UIWindowDidBecomeHiddenNotification中移除方向旋转的监听,并将设备旋转到竖直方向

一、监听网页上的视频播放

//点击了视频播放按钮,会进入系统原生的播放器
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(beginPlayVideo:) 
name:UIWindowDidBecomeVisibleNotification object:nil];
//点击原生的系统播放器的左上角的关闭按钮的回调
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(endPlayVideo:)               
name:UIWindowDidBecomeHiddenNotification object:nil];

二、监听设备旋转的方向(此方法生效需要关闭屏幕锁定)

[[UIDevice currentDevice]
beginGeneratingDeviceOrientationNotifications];   
[[NSNotificationCenter defaultCenter] addObserver:self 
 selector:@selector(onDeviceOrientationChange) 
 name:UIDeviceOrientationDidChangeNotification object:nil];

三、实现屏幕旋转的方法

//设置屏幕的旋转
 // @param orientation  旋转的方向
-(void)toOrientation:(UIInterfaceOrientation)orientation {
UIApplication *application= [UIApplication sharedApplication];
// 判断如果当前方向和要旋转的方向一致,那么不做任何操作
if (self.lastOrientation == orientation) { return; }
 UIWindow *keyWindow = application.keyWindow;
if(systemVersion()<13){
    [application setStatusBarOrientation:orientation animated:NO];
}
  if (orientation != UIInterfaceOrientationPortrait) {//横屏
      if(systemVersion()>=13){
         [application setStatusBarHidden:YES];
      }

        keyWindow.bounds=CGRectMake(0, 0, ScreenHeight(), ScreenWidth());
        self.fullScreen=YES;
  }else{
      if(systemVersion()>=13){
          [application setStatusBarHidden:NO];
      }
      keyWindow.bounds=CGRectMake(0, 0, ScreenWidth(),ScreenHeight());
      self.fullScreen=NO;
  }
  [UIView beginAnimations:nil context:nil];
// 获取旋转状态条需要的时间:
  [UIView setAnimationDuration:0.3];

// 设置给你播放视频的视图的方向设置旋转
keyWindow.transform = [self getTransformRotationAngle:orientation];
// 开始旋转
 [UIView commitAnimations];
self.lastOrientation=orientation;
}

获取旋转的角度

 /**
 * 获取变换的旋转角度
* @return 角度
 */
- (CGAffineTransform)getTransformRotationAngle:(UIInterfaceOrientation)orientation {
// 根据要进行旋转的方向来计算旋转的角度
if (orientation == UIInterfaceOrientationPortrait) {
    return CGAffineTransformIdentity;
} else if (orientation == UIInterfaceOrientationLandscapeLeft){
    return CGAffineTransformMakeRotation(-M_PI_2);
} else if(orientation == UIInterfaceOrientationLandscapeRight){
    return CGAffineTransformMakeRotation(M_PI_2);
}
return CGAffineTransformIdentity;
}

使用:

(1)只需引入"UIViewController+XXTVideoPlay.h"

(2)在videoLoad方法中添加监听方法

[self  observerWebViewVideoPlay];

(3) 最后需要在viewDidDisappear方法移除监听

[self removeObserverWebViewVideoPlay ];

具体的代码请前往gitup查看

最后附上git地址https://github.com/zhaojinjie/XXTWebVideoRotate

相关文章

网友评论

      本文标题:iOSWebView播放视频进行屏幕旋转

      本文链接:https://www.haomeiwen.com/subject/ziqghhtx.html