【问题标题】:Allow fullscreen landscape only for system AVPlayerViewController仅允许系统 AVPlayerViewController 的全屏横向
【发布时间】:2017-03-26 02:54:31
【问题描述】:

我有一个应用程序,它使用UISplitViewController 在具有WKWebView 属性的详细视图控制器中加载网页。一些 URL 是嵌入的 YouTube 视频的链接,一些是 .mp4 文件的直接链接。无论哪种方式,视频文件都会自动加载(在点击 YouTube 视频之后)到全屏系统播放器中,我假设它是 AVPlayerViewController。我看过几篇关于子类化AVPlayerViewController 以通过实现supportedInterfaceOrientations 来允许轮换的帖子,以及其他建议在application: supportedInterfaceOrientationsForWindow: 中检查UIWindowrootViewController presentedViewController 的类或检查AppDelegate 上的变量是在呈现 AVPlayerViewController 时设置的,但是这些解决方案都没有对我有用,因为我没有创建或呈现我自己的 AVPlayerViewController 实例,所以我不确定如何播放这些视频时允许横向旋转。

有什么方法可以让我知道系统何时以全屏模式播放视频,以便允许旋转?

以下是我已经看过的一些帖子的链接:

MPMoviePlayerViewController | Allow landscape mode

Only ONE VIEW landscape mode

【问题讨论】:

    标签: ios objective-c rotation avplayer


    【解决方案1】:

    我希望你仍然需要一个答案。我在 Swift 中找到了解决方案,但它可以在 Objective C 中简单地转换。我发现,当使用 WKWebView(也可能是 UIWebView)时,全屏视频会在新的 UIWindow 中呈现(至少在 iOS 10 上)。该窗口有空白的 UIViewController 并在其上显示 AVFullScreenViewController

    所以,在你的 AppDelegate 中你应该像这样实现application:supportedInterfaceOrientationsForWindow:

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if window != self.window, let pvc = window?.rootViewController?.presentedViewController, "\(type(of: pvc))" == "AVFullScreenViewController" {
            return pvc.isBeingDismissed ? .portrait : .all
        }
        return .portrait
    }
    

    虽然 AVFullScreenViewController 是私有 API 类,但您可以保护自己并将 "AVFullScreenViewController" 替换为 String(format: "AV%@ViewController", "FullScreen")

    祝你好运!

    【讨论】:

    • 我也不得不使用UIDevice.current.setValue(UIInterfaceOrientationMask.portriat.rawValue, forKey: "orientation")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2014-12-23
    相关资源
    最近更新 更多