Properly force or allow Landscape mode for Youtube embedded video in iOS 8 without NSNotificationCenter

做~自己de王妃 提交于 2019-12-05 09:13:42

So, after some research and looking more in-depth to this problem.. I came to a solution using the UIWebView delegates, plus I had to solve another issue in terms of my function - (void)playerEnded which it wasn't working properly in the new iPhone 6 devices..

This is how I did it.. First, in my webViewDidFinishLoad method I have added to my webview a javascript evaluation to check when this video player goes into fullscreen mode..

- (void)webViewDidFinishLoad:(UIWebView*)webView
{
    // adding listener to webView
    [_webView stringByEvaluatingJavaScriptFromString:@" for (var i = 0, videos = document.getElementsByTagName('video'); i < videos.length; i++) {"
                                                     @"      videos[i].addEventListener('webkitbeginfullscreen', function(){ "
                                                     @"           window.location = 'videohandler://begin-fullscreen';"
                                                     @"      }, false);"
                                                     @""
                                                     @"      videos[i].addEventListener('webkitendfullscreen', function(){ "
                                                     @"           window.location = 'videohandler://end-fullscreen';"
                                                     @"      }, false);"
                                                     @" }"
                                                     ];
}

Then, in my - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType method, I check when my request url matches the state of the youtube player, like this.. This will fire our function to allow landscape mode or force back to portrait mode.. or maybe any other type of work you might want to do..

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
    // allows youtube player in landscape mode
    if ([request.URL.absoluteString isEqualToString:@"ytplayer://onStateChange?data=3"])
    {
        [self playerStarted];

        return NO;
    }
    if ([request.URL.absoluteString isEqualToString:@"ytplayer://onStateChange?data=2"])
    {
        [self playerEnded];

        return NO;
    }
}

And finally, I needed to adjust my playerEnded function to force back portrait mode for iPhone 6 devices..

- (void)playerEnded
{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

    ((AppDelegate*)[[UIApplication sharedApplication] delegate]).videoIsInFullscreen = NO;

    [self supportedInterfaceOrientations];

    [self shouldAutorotate:UIInterfaceOrientationPortrait];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}

Almost, missed I also added these two other functions..

- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

So, finally I am able to catch the state of the actual player and fire my functions to do some work or whatever I want at the right moment, in my case changing the orientation..

I hope this helps someone else..

I am working on swift, I my player runs movie in both portrait and landscape direction. First I checked three modes : portrait, landscapeleft, landscaperight. Second I wrote this function in all viewController:

 isFullScreen = false
override func shouldAutorotate() -> Bool {
    if isFullScreen == true {
        return true
    }else{
        return false
    }
}

Third I change the value of isFullScreen in this function:

func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState) {
    switch (state) {
        case  YTPlayerState.Playing:
                println("started to play")
        isFullScreen == true
        shouldAutorotate()
        case YTPlayerState.Paused:
                println("paused")
        default:
            println("non of sttate")
        break
    }
}

And video runs on both portrait and landscape mode! The interesting thing is that I dont set isFullScreen to false again when I pause video or move from fullscreen. However it doesnt rotate! Can somebody explain this?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!