禁止自动横屏下的视频播放强制旋转

栏目: IOS · 发布时间: 6年前

内容简介:这个标题有点绕,所以我来解释一下。之前项目开发的时候需要用到一个视频播放的第三方库,但是这个第三方库在从小屏幕播放切换到大屏幕播放的时候,是需要开启项目的自动旋转支持的。也就是

这个标题有点绕,所以我来解释一下。

之前项目开发的时候需要用到一个视频播放的第三方库,但是这个第三方库在从小屏幕播放切换到大屏幕播放的时候,是需要开启项目的自动旋转支持的。也就是

禁止自动横屏下的视频播放强制旋转

第三方视频播放库 BMPlayer 这个库中有许多细节的部分无法满足实际使用,因此我下了源码后自己根据需求修改。

然而实际应用中,许多的 App 的要求是需要禁止横屏的。因为大多数 App(窃以为除了游戏以外的)即使写好了约束,也都不会针对横屏下的 UI 作对应的优化。 因此我们需要自己针对 工程禁用横屏情况下,播放视频时切换到全屏做自己的优化

问题的扩展

之前搜索播放框架的时候没有找到适当的,因此最后决定自己把 BMPlayer 的源码 down 下来自己修改。虽然最后解决了(这也是本篇文章下面主要阐述的内容),但是解决方案是在 AppDelegate 中增加一个变量来控制当前 App 的方向。实际上还是增加了库和工程之间的耦合度。暂时还未找到更好的解决方案。

实现的效果

禁止自动横屏下的视频播放强制旋转

代码和说明( Swift 为例)

下面的源码中略去了不必要的代码

AppDelegate.swift 中

let appDelegate = UIApplication.shared.delegate as! AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, WXApiDelegate {
    var deviceOrientation = UIInterfaceOrientationMask.portrait
    
    // 去实现这个回调代理
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return deviceOrientation
    }
}
复制代码

在视频播放库中控制它的方向

点击全屏播放和恢复小屏幕时候两个按钮的对应方法

private func forceToPlayOnFullScreen() {
    let currentOrientation = UIDevice.current.orientation
    print("向右旋转前 当前的方向 = ", currentOrientation.rawValue)
    
    appDelegate.deviceOrientation = .landscapeRight
    // 如果防止用户在进入当前页面时手机横置,那么当前的 device orientation 是 landscapeRight
    // 这时候,我们再去设置 landscapeRight,是不会引起广播 "UIDeviceOrientationDidChangeNotification",因此手动调用一次
    let portraitValue = UIInterfaceOrientation.portrait.rawValue
    UIDevice.current.setValue(portraitValue, forKey: "orientation")
    
    let rightValue = UIInterfaceOrientation.landscapeRight.rawValue
    UIDevice.current.setValue(rightValue, forKey: "orientation")
    
}

private func forceToPlayOnOriginScreen() {
    let currentOrientation = UIDevice.current.orientation
    print("恢复垂直前当前的方向 = ", currentOrientation.rawValue)
    
    // 这里为什么要写两遍,在下面说明
    appDelegate.deviceOrientation = .portrait
    let rightValue = UIInterfaceOrientation.landscapeRight.rawValue
    UIDevice.current.setValue(rightValue, forKey: "orientation")
    
    let portraitValue = UIInterfaceOrientation.portrait.rawValue
    UIDevice.current.setValue(portraitValue, forKey: "orientation")
    
}

复制代码

关于代码设置了两次方向的说明

一定很奇怪,为什么两次代码的调用中,设置方向的代码要写两遍呢

UIDevice.current.setValue

这是因为,视频播放库,这里需要去获取一个系统的广播 UIDeviceOrientationDidChangeNotification .

说明

假设我们当前的屏幕是横屏的情况

禁止自动横屏下的视频播放强制旋转

此时系统会利用陀螺仪,虽然我们视觉上看上去,他并没有向右旋转,但是当前的方向 UIDevice.current.orientation 会变成 landScapeRight .

因此我们如果这时候去调用

let rightValue = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(rightValue, forKey: "orientation")
复制代码

这个函数的时候,不会触发广播,因为系统会认为,其实我的方向没有变化啊!

这也就是为什么上面我们会先让他的方向设为默认的 portrait 再切回 landscapeRight . 我曾经误以为界面会不会旋转回去一次再赚回来,但是实际测试发现倒是没有出现类似的情景。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pattern Recognition and Machine Learning

Pattern Recognition and Machine Learning

Christopher Bishop / Springer / 2007-10-1 / USD 94.95

The dramatic growth in practical applications for machine learning over the last ten years has been accompanied by many important developments in the underlying algorithms and techniques. For example,......一起来看看 《Pattern Recognition and Machine Learning》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

SHA 加密
SHA 加密

SHA 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具