开发者

Play YouTube video in UIWebView in landscape orientation while ViewController is in Portrait Orientation

开发者 https://www.devze.com 2023-03-10 03:07 出处:网络
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.

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 someo开发者_Python百科ne please help me out?

Thanks!


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
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号