0

我有在 Android 上的 WebView 中播放 YouTube 的代码。它在新手机(Android 4.4、5.1、6、7 OS)上运行良好,但是当我在 Android 4.0.3 上尝试它时,它会打开带有控件的 YouTube 框架,然后单击中间的按钮播放后,灰色背景会显示所有时间。

编码:

private void initializeWebView() {    

    WebView webView = (WebView) findViewById(R.id.webView);

    //webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    webView.setBackgroundColor(getResources().getColor(R.color.semi_transparent_black_20percentage));  
    webView.getSettings().setBuiltInZoomControls(true);
    //webView.getSettings().setDisplayZoomControls(false);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    WebSettings webSetting = webView.getSettings();
    webSetting.setJavaScriptEnabled(true);
    webSetting.setDisplayZoomControls(false);

    //webSetting.setLoadWithOverviewMode(true);
    //webSetting.setUseWideViewPort(true);
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    webView.setLayoutParams(new RelativeLayout.LayoutParams( screenWidth / 2, LayoutParams.MATCH_PARENT));
     getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webSetting.setJavaScriptEnabled(true);
    webSetting.setBuiltInZoomControls(true);
    webSetting.setAllowContentAccess(true);
    webSetting.setEnableSmoothTransition(true);
    webSetting.setLoadsImagesAutomatically(true);
    webSetting.setLoadWithOverviewMode(true);
    webSetting.setSupportZoom(false);
    webSetting.setUseWideViewPort(true);
    webSetting.setAppCacheEnabled(true);
    webSetting.setSupportMultipleWindows(true);

    webView.setVisibility(View.VISIBLE);
}   



public void  loadPageIntoWebView(String htmlFilename){

    AssetManager mgr = getBaseContext().getAssets();
    try {
        String htmlContentInStringFormat = "";
        WebView webView = (WebView) findViewById(R.id.webView);
        String frameVideo = "<body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/2a7f29Jvihc?wmode=transparent\" frameborder=\"0\" allowtransparency></iframe></body>";
        htmlContentInStringFormat = frameVideo;
        webView.loadDataWithBaseURL(null, htmlContentInStringFormat, "text/html", "utf-8", null);
    } catch (Exception e) {
        e.printStackTrace();
    }      
}

此代码运行良好,可在较新的手机(Android OS4.4-7)上播放 YouTube 视频。

我应该改变什么才能让它在 Android 4.0.3 上运行?

这是在单击屏幕中间的播放按钮后显示灰色背景但未播放 youtubevideo 的照片:

4

1 回答 1

0

解决了!

在我添加以下行后,它也适用于 Android 4.0.3:

    webView.setWebChromeClient(new WebChromeClient() {});

这不是强制性的,但我在此行之前添加了一行:webView.setWebViewClient(new WebViewClient()

希望这个解决方案能帮助其他使用Epson Moverio BT-200智能眼镜等的同事。

;-)

于 2017-12-12T16:46:20.123 回答