在 UIWebView 中以横向方式播放 YouTube 视频,而 ViewController 则以纵向方式播放

发布于 2024-11-14 08:09:41 字数 178 浏览 2 评论 0原文

我正在开发一个需要播放 YouTube 视频的 iPhone 应用程序。

我有一个纵向方向的 ViewController,仅具有加载 YouTube 视频的 UIWebView。

我想做的是视频可以横向播放。

这听起来很简单,但我不知道如何做到这一点。有人可以帮我吗?

谢谢!

I am developing an iPhone application that needs to play a YouTube video.

I have a ViewController in Portrait Orientation only with a UIWebView that loads a YouTube Video.

What I am trying to do is that the video can be played in landscape orientation.

This sounds pretty simple but I can't figure out how to do that. Could someone please help me out?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢烬 2024-11-21 08:09:41

这在 AppDelegate 中。仅为视频播放器启用水平模式

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if let presentedViewController = window?.rootViewController?.presentedViewController {

        let className = String(describing: type(of: presentedViewController))
        if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className)
        {
            return UIInterfaceOrientationMask.allButUpsideDown
        }

    }
    return UIInterfaceOrientationMask.portrait
}

This goes in the AppDelegate. Will enable horizontal mode for video players only

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if let presentedViewController = window?.rootViewController?.presentedViewController {

        let className = String(describing: type(of: presentedViewController))
        if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className)
        {
            return UIInterfaceOrientationMask.allButUpsideDown
        }

    }
    return UIInterfaceOrientationMask.portrait
}
有木有妳兜一样 2024-11-21 08:09:41

目前无法以编程方式开始播放 YouTube 视频。
也就是说,您可以在 ViewController 的 willRotateToInterfaceOrientation:duration: 中创建一个 UIWebview,检查水平方向,并使用显示视频缩略图的代码显示 Web 视图。但用户仍然需要激活视图。
要删除视频,您可以使用相同的方法执行此操作,但对于纵向方向并删除网络视图。

There is currently no way to start playing a YouTube video programmatically.
That said, you can make a UIWebview in the ViewController's willRotateToInterfaceOrientation:duration: checking for the horizontal orientation and display the webview with the code that shows the video's thumbnail. But the user will still have to activate the view.
To remove the video you do it in the same method, but for the portrait orientation and remove the webview.

寂寞花火° 2024-11-21 08:09:41

我找到了答案。

1) 我将此代码添加到我的 viewController.m

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

2) 在界面生成器中,我在 Landscape 中构建界面,每个元素旋转 90°。

当我构建并运行时,视图似乎处于纵向模式,而实际上是横向模式。因此,UIWebView 中嵌入的 YouTube 视频以横向方式播放。

I found the answer.

1) I add this code to my viewController.m

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

2) In interface builder I build my interface in Landscape, with every single element rotated by 90°.

When I build and Run, the view seems to be in portrait mode when it is in fact landscape. Therefore, the YouTube Video embedded in the UIWebView plays in Landscape.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文