Rotate screen when HTML5 video playing in fullscreen
Asked Answered
T

1

4

First of all, i would like to apologize for my bad level in english, and for all the mistakes i might have done in this post. I'm also a "junior" in Android devloppement (i'm doing this on my free time), i'm not good and experienced enough yet ^^ so sorry if i might have some difficulties to understands the answer you might give me.

So what i'm doing ? I'm creating a simple Android App. It's just a Webview to load my website. My website is just a random Youtube video player.

First i had the problem to play HTML5 Video in Fullscreen. I succeed to done that with this project VideoEnabledWebView

So my app is exactly the same as VideoEnabledWebView.

What is my problem ? I block the screen rotation in my app. Only "Portrait" mode and i succeed to done that. But when i play a video in fullscreen mode, i can't be in landscape mode. I understand why, but i can't find a solution, a hack to able to do that. So i would like to rotate the screen in landscape mode only if i'm playing the video in fullscreen.

First, Here is my MainActivity.java :

public class MainActivity extends ActionBarActivity
{
    private VideoEnabledWebView webView;
    private VideoEnabledWebChromeClient webChromeClient;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        webView = (VideoEnabledWebView)findViewById(R.id.webView);

        View nonVideoLayout = findViewById(R.id.nonVideoLayout); // Your own view, read class comments
        ViewGroup videoLayout = (ViewGroup)findViewById(R.id.videoLayout);

        View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments
        webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
        {                
            @Override
            public void onProgressChanged(WebView view, int progress)
            {

            }
        };
        webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
        {
            @Override
            public void toggledFullscreen(boolean fullscreen)
            {
                if (fullscreen)
                {
                    WindowManager.LayoutParams attrs = getWindow().getAttributes();
                    attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14)
                    {
                        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                    }
                }
                else
                {
                    WindowManager.LayoutParams attrs = getWindow().getAttributes();
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14)
                    {
                        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                    }
                }

            }
        });
        webView.setWebChromeClient(webChromeClient);
        webView.setWebViewClient(new myWebViewClient());

        webView.loadUrl("https://www.awesomekraken.com/fr");
    }

    public class myWebViewClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onBackPressed()
    {
        if (!webChromeClient.onBackPressed())
        {
            if (webView.canGoBack())
            {
                webView.goBack();
            }
            else
            {
                super.onBackPressed();
            }
        }
    }
}

As you can see in the onCreate method, i put the line :

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

I think it's in this part i can resolve my problem :

 webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
        {
            @Override
            public void toggledFullscreen(boolean fullscreen)
            {
                if (fullscreen)
                {
                    WindowManager.LayoutParams attrs = getWindow().getAttributes();
                    attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14)
                    {
                        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                    }
                }
                else
                {
                    WindowManager.LayoutParams attrs = getWindow().getAttributes();
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14)
                    {
                        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                    }
                }

            }
        });

I tried to put setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); under the if(fullscreen) but that doesn't work. The app switch in Landscape for a half second and the webview reload the all page.

I searched a lot on Google & StackOverflow but from now i found nothing very helpfull.

Only this post is the same as mine, but he got no any answer.

If anyone have a solution, tip or advice ...

Of course, i will update this post if i get more information, or solve my problem.

Thanks in advance for your help !

Thermaesthesia answered 8/4, 2015 at 14:40 Comment(3)
Hey, did you ever solve this problem? I am stuck on this too.Scruffy
Hello, sorry for the late answer ... I did found a solution but i don't remember how, i don't have the source code anymore. Maybe you should try this : #15769337Fucus
yeah i ended up figuring it out too. my code is kind of crazy though. i ended up having to use javascript and stuff. but it worksScruffy
F
1

I ended up in rotating the custom view container:

public class RotatedVerticalFrameLayout extends FrameLayout{

    public RotatedVerticalFrameLayout(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @SuppressWarnings("SuspiciousNameCombination")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        int w = getWidth();
        int h = getHeight();

        setRotation(90.0f);
        setTranslationX((h - w) / 2);
        setTranslationY((w - h) / 2);

        ViewGroup.LayoutParams lp = getLayoutParams();
        lp.height = w;
        lp.width = h;
        requestLayout();
    }
}

This looks like landscape mode if you only show the custom view instead of actionbar and other contents.

Feingold answered 27/10, 2016 at 8:33 Comment(1)
I find this answer incomplete, but another answer: https://mcmap.net/q/1815038/-how-to-play-video-fullscreen-with-landscape-in-android-webview , completes it.Overshadow

© 2022 - 2024 — McMap. All rights reserved.