Youtube video played in WebView shows black screen when taken to fullscreen

孤街醉人 提交于 2019-12-08 02:45:00

问题


Short Version: To play YouTube video, I'm using a WebView like this,

webView.loadData("<style> body{margin:0; background-color:black;}div{width:100%; height:100%; background-color:black}iframe{ width:100%; height:100%; border:none; overflow:hidden; }</style><div><iframe src=\"http://www.youtube-nocookie.com/embed/4eO9SS3wvLY?autoplay=1&rel=0&app=youtube_gdata&fs=1" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" webkitallowfullscreen=\"webkitallowfullscreen\"></iframe></div>", "text/html", "utf-8");

Everything works fine except when clicking on [ ] icon to take the video to full screen, it shows black screen on the first, second and third attempt. On the fourth attempt, more often than not, it properly shows the video in fullscreen. However, it is not always the case.

Note: For twitter and facebook videos, taking to fullscreen works as expected.

Long Version:

The code I am using to achieve this is,

public class Test1Activity extends AppCompatActivity {

ViewGroup rootView;

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

    rootView = findViewById(R.id.container);

    WebView webView = findViewById(R.id.web_view);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient() {

        private View viewTakingFullScreen;

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            //Log.d(TAG, "onShowCustomView(View:" + view + ", CustomViewCallback:" + callback + ")");
            super.onShowCustomView(view, callback);

            if (view instanceof FrameLayout) {
                // a video wants to be shown fullscreen
                viewTakingFullScreen = view;
                WindowManager.LayoutParams attrs = getWindow().getAttributes();
                attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                getWindow().setAttributes(attrs);
                rootView.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            }


        }

        @Override
        public void onHideCustomView() {
            //Log.d(TAG, "onHideCustomView()");
            super.onHideCustomView();

            if (viewTakingFullScreen != null) {
                WindowManager.LayoutParams attrs = getWindow().getAttributes();
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                getWindow().setAttributes(attrs);
                int index = rootView.indexOfChild(viewTakingFullScreen);
                rootView.removeViewAt(index);
                viewTakingFullScreen = null;
            }
        }
    });

    String htmlContent = "<style> body{margin:0; background-color:black;}div{width:100%; height:100%; background-color:black}iframe{ width:100%; height:100%; border:none; overflow:hidden; }</style><div><iframe src=\"http://www.youtube-nocookie.com/embed/4eO9SS3wvLY?autoplay=1&rel=0&app=youtube_gdata&fs=1\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" webkitallowfullscreen=\"webkitallowfullscreen\"></iframe></div>";
    webView.loadData(htmlContent, "text/html", "utf-8");
    //webView.loadUrl("http://www.youtube-nocookie.com/embed/4eO9SS3wvLY?autoplay=1&rel=0&app=youtube_gdata&fs=1");
    }

}

This is a layout (activity_test1.xml) file I'm using,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Top Button" />

        <WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="450dp">

        </WebView>

    </LinearLayout>

</FrameLayout>

I have tried debugging this for two days now but in vain. I feel the issue might be with using YouTube embed player inside a <iframe> tag. But I see no indication of error in the logs.

Also, I am using <iframe> to play Twitter and Facebook videos too. But Twitter video has <video> tag and Facebook video has it's own embed player (https://www.facebook.com/video/embed?video_id={id}). They seem to work fine in fullscreen.

来源:https://stackoverflow.com/questions/52085952/youtube-video-played-in-webview-shows-black-screen-when-taken-to-fullscreen

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