在WebView中播放的Youtube视频在全屏播放时显示黑屏

时间:2018-08-29 20:55:54

标签: android youtube-api android-webview android-youtube-api android-fullscreen

短版:要播放YouTube视频,我使用的是这样的WebView,

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");

一切正常,除了单击[]图标以使视频全屏显示外,第一次,第二次和第三次尝试均显示黑屏。在第四次尝试中,通常会全屏正常显示视频。但是,并非总是如此。

注意:对于Twitter和Facebook视频,按预期可全屏显示。

长版:

我用来实现此目的的代码是

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");
    }

}

这是我正在使用的布局(activity_test1.xml)文件,

<?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>

我已经尝试调试了两天,但徒劳无功。我觉得问题可能在于在<iframe>标签内使用YouTube嵌入播放器。但是我看不到日志中有错误的迹象。

此外,我也使用<iframe>来播放Twitter和Facebook视频。但Twitter视频具有<video>标签,而Facebook视频具有其自己的嵌入播放器(https://www.facebook.com/video/embed?video_id= {id})。他们似乎在全屏模式下都能正常工作。

0 个答案:

没有答案
相关问题