菜鸟教程小白 发表于 2022-12-12 22:47:53

ios - 仅允许系统 AVPlayerViewController 的全屏横向


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

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

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

<p> <a href="https://stackoverflow.com/questions/17276898/mpmovieplayerviewcontroller-allow-landscape-mode/26025794#26025794" rel="noreferrer noopener nofollow">MPMoviePlayerViewController | Allow landscape mode</a> </p>

<p> <a href="https://stackoverflow.com/questions/24928057/only-one-view-landscape-mode" rel="noreferrer noopener nofollow">Only ONE VIEW landscape mode</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我希望你仍然需要一个答案。我在 Swift 中找到了解决方案,但它可以在 Objective C 中简单地转换。我发现,当使用 WKWebView(也可能是 UIWebView)时,全屏视频会在新的 UIWindow 中呈现(至少在 iOS 10 上)。该窗口有空白的 <strong>UIViewController</strong> 并在其上显示 <strong>AVFullScreenViewController</strong>。</p>

<p>所以,在你的 AppDelegate 中你应该像这样实现 <code>application:supportedInterfaceOrientationsForWindow:</code></p>

<pre><code>func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -&gt; UIInterfaceOrientationMask {
    if window != self.window, let pvc = window?.rootViewController?.presentedViewController, &#34;\(type(of: pvc))&#34; == &#34;AVFullScreenViewController&#34; {
      return pvc.isBeingDismissed ? .portrait : .all
    }
    return .portrait
}
</code></pre>

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

<p>祝你好运!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 仅允许系统 AVPlayerViewController 的全屏横向,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40555385/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40555385/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 仅允许系统 AVPlayerViewController 的全屏横向