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

问题的由来

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

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

图一

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

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

问题的扩展

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

实现的效果

效果图.gif

代码和说明(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.

说明

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

以 Bilibili 的播放页面为例

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

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

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

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

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

猜你喜欢

转载自juejin.im/post/5bf1727e6fb9a04a0c2e094d