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

Android 在WebView中播放的Youtube视频在全屏播放时显示黑屏,android,youtube-api,android-webview,android-youtube-api,android-fullscreen,Android,Youtube Api,Android Webview,Android Youtube Api,Android Fullscreen,短版本:要播放YouTube视频,我使用的是这样的网络视图 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><ifra

短版本:要播放YouTube视频,我使用的是这样的网络视图

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");
webView.loadData(“正文{margin:0;背景色:黑色;}div{width:100%;高度:100%;背景色:黑色}iframe{width:100%;高度:100%;边框:无;溢出:隐藏;}”,“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");
    }

}
公共类Test1活动扩展了AppCompative活动{
视图组根视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
rootView=findviewbyd(R.id.container);
WebView WebView=findviewbyd(R.id.web\u视图);
webView.getSettings().setJavaScriptEnabled(true);
setWebView.WebChromeClient(新WebChromeClient(){
私密视图全屏取景;
@凌驾
ShowCustomView(视图视图、CustomViewCallback回调)上的公共无效{
//Log.d(标记“onShowCustomView(视图:“+View+”,CustomViewCallback:“+callback+”));
super.onShowCustomView(查看、回调);
if(查看FrameLayout的实例){
//一个视频需要全屏显示
viewTakingFullScreen=查看;
WindowManager.LayoutParams attrs=getWindow().getAttributes();
attrs.flags |=WindowManager.LayoutParams.FLAG_全屏显示;
attrs.flags |=WindowManager.LayoutParams.FLAG_保持屏幕打开;
getWindow().setAttributes(属性);
rootView.addView(视图,新视图组.LayoutParams(视图组.LayoutParams.MATCH_父级,视图组.LayoutParams.MATCH_父级));
}
}
@凌驾
公共无效onHideCustomView(){
//Log.d(标记“onHideCustomView()”);
super.onHideCustomView();
如果(取景全屏!=null){
WindowManager.LayoutParams attrs=getWindow().getAttributes();
attrs.flags&=~WindowManager.LayoutParams.FLAG_全屏显示;
attrs.flags&=~WindowManager.LayoutParams.FLAG\u保持屏幕打开;
getWindow().setAttributes(属性);
int index=rootView.indexOfChild(查看全屏);
rootView.removeViewAt(索引);
viewTakingFullScreen=null;
}
}
});
字符串htmlContent=“正文{margin:0;背景色:黑色;}div{width:100%;高度:100%;背景色:黑色}iframe{width:100%;高度:100%;边框:无;溢出:隐藏;}”;
加载数据(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>

我已经试着调试了两天,但都没有成功。我觉得问题可能在于在
标签中使用YouTube嵌入式播放器。但是我在日志中没有看到错误的迹象

此外,我还使用
播放Twitter和Facebook视频。但是Twitter视频有
标签,Facebook视频有自己的嵌入式播放器({id})。它们在全屏上似乎工作得很好