How to embed Youtube video in Android WebView?

前端 未结 3 983
广开言路
广开言路 2021-02-02 04:34

I want to embed a youtube video in my Android app. It has a Navigation Drawer, and when this drawer is opened, the video must to keep playing. And I\'d like my app could be capa

3条回答
  •  再見小時候
    2021-02-02 04:59

    For my case Android 4.4.4, adding the Referer header "https://www.youtube.com" in first argument of loadDataWithBaseURL() saved my day!

        mWebView = (WebView) getActivity().findViewById(R.id.webView);
    
        if (Build.VERSION.SDK_INT < 8) {
            mWebView.getSettings().setPluginsEnabled(true);
        } else {
            mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
        }
    
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setBackgroundColor(0x00000000);
        mWebView.getSettings().setBuiltInZoomControls(true);
        String html = "";
        mWebView.loadDataWithBaseURL("https://www.youtube.com", html, "text/html", "UTF-8", null);
    

提交回复
热议问题