0

I have the code for playing YouTube in WebView on Android. It works well on new phones (Android 4.4, 5.1, 6, 7 OS) but when I tried it on Android 4.0.3 it opens the YouTube frame with controls and after clicking on button play in the midle the gray background is shown all the time.

The code:

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

This code is working well and playing YouTube videos on newer phones (Android OS4.4-7).

What I should change to get it working on Android 4.0.3 as well ?

this is the photo showing the gray background without playing youtubevideo after clicking on the button play inthe middle of the screen:

1 Answers1

0

Solved!

After I added the following line it works on Android 4.0.3 aswell:

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

It is not mandatory but I added the line before this line: webView.setWebViewClient(new WebViewClient()

Hope that this solution will help other collegues working with Epson Moverio BT-200 smart glasses etc.

;-)