How to embed Youtube video in Android WebView?

前端 未结 3 982
广开言路
广开言路 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:58

    I recently met the same problem and solved it, hope to be helpful.

    It's because this video is not playable on all websites, so if you load a local html, the "Referer" http header is empty, then youtube can't determine if your website is on the banned list. The solution is just put your html on your own web server, then load it through url, then the "Referer" header will be your web server address, problem solved.

    0 讨论(0)
  • 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 = "<iframe height='95%' width='100%' src='" + videoURL + "' frameborder='0' allowfullscreen></iframe>";
        mWebView.loadDataWithBaseURL("https://www.youtube.com", html, "text/html", "UTF-8", null);
    
    0 讨论(0)
  • 2021-02-02 05:07

    I think that a similar question that has been asked might be of help to you and might be a good starting point. But the second link I am going to post might be of more help.

    Play Youtube HTML5 embedded Video in Android WebView

    Also, I found on a separate website this comment that might be your magic answer:

    "The custom player you are mentioning most likely uses a rather well-known trick; fooling youtube into providing an MP4-version of that video, download it progressively and serve it back via a local http-server to MPMoviePlayerController.

    The restriction is based on a reverse lookup of the IP address you are using when accessing the video in question. Use a proxy from within the US and you will see that it suddenly plays without any problem." http://w3facility.org/question/embed-youtube-videos-with-contains-content-from-it-is-restricted-from-playback-on-certain-site/

    I hope this helps!!

    0 讨论(0)
提交回复
热议问题