iOS中仅当视屏全屏播放时支持视屏旋转

开发环境:XCode7.2 

项目设置:Device Orientation仅勾选Portrait    Deployment Target7.0  使用MediaPlayer

需求:在视屏进入全屏播放时,视屏视图旋转,达到真的全屏播放

实现:①在AppDelegate.h文件中声明一个BOOL类型的变量,记录是否允许旋转:

             @property (nonatomic, assign) BOOL  allowRotation;

          ②在AppDelegate.m文件中添加方法内容:(方法为系统提供)

            - (UIInterfaceOrientationMask) application: (UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

                       if(self.allowRotation) {

                               //无特殊要求,故直接返回支持所有方向旋转

                               return UIInterfaceOrientationMaskAll;

                      }else{

                    //当不允许旋转的时候,只支持默认的方向              

                               return UIInterfaceOrientationMaskPortrait;

                     }

           }

        ③在有播放器的viewController中导入AppDelegate

            #import"AppDelegate.h"

        ④添加观察者方法:(自己定义,定以后可在viewDidLoad中调用)

           - (void)addNotification {

                    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

                   [center addObserver:self selector:@selector(willEnterFullScreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];//进入全屏的通知

                  [center addObserver:self selector:@selector(willExitFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];//退出全屏的通知

}

- (void)willEnterFullScreen:(NSNotification*)notification

{

       AppDelegate*delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

      //设置在进入全屏时,允许旋转

      delegate.allowRotation=YES;

}

- (void)willExitFullScreen:(NSNotification*)notification

{

      AppDelegate*delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

     //设置退出全屏模式禁止旋转

      delegate.allowRotation=NO;

}

//释放观察者

- (void)dealloc {

      [[NSNotificationCenter defaultCenter] removeObserver:self];

}

通过以上方法达到了项目仅支持原始方向的竖屏,但当播放器进入全屏模式的时候,可以旋转播放器,使视频全屏播放

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS上,<video>标签默认是不支持自动全屏播放的。但是,你可以通过以下方式实现自动全屏播放,并且在横屏自动切换到横屏模式: 1. 在<video>标签中添加webkit-playsinline属性,这将允许视频在页面内播放而不是全屏播放。 ``` <video src="video.mp4" webkit-playsinline></video> ``` 2. 为了实现自动全屏播放,你需要使用JavaScript来控制<video>标签。在页面加载完成后,使用以下代码将视频播放: ``` var video = document.querySelector('video'); video.play(); ``` 3. 为了在横屏自动切换到横屏模式,你需要在JavaScript中监听屏幕旋转事件(orientationchange),并在事件发生修改<video>标签的样式,将其宽度设置为屏幕的高度,将其高度设置为屏幕的宽度,同还需要将页面的viewport元标签设置为允许横屏显示: ``` //监听屏幕旋转事件 window.addEventListener("orientationchange", function() { if (window.orientation == 90 || window.orientation == -90) { //横屏视频全屏,并修改viewport元标签 document.querySelector('video').style.width = window.innerHeight + 'px'; document.querySelector('video').style.height = window.innerWidth + 'px'; document.querySelector('meta[name=viewport]').setAttribute('content', 'width=device-height, initial-scale=1.0'); } else { //竖屏视频恢复原始尺寸,并修改viewport元标签 document.querySelector('video').style.width = '100%'; document.querySelector('video').style.height = 'auto'; document.querySelector('meta[name=viewport]').setAttribute('content', 'width=device-width, initial-scale=1.0'); } }); ``` 这样就可以实现iOS上<video>标签的自动全屏播放,并且在横屏自动切换到横屏模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值