Rotate screen when HTML5 video playing in fullscreen

99封情书 提交于 2019-12-22 09:33:39

问题


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 !


回答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.



来源:https://stackoverflow.com/questions/29517829/rotate-screen-when-html5-video-playing-in-fullscreen

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