How to play dailymotion video inside a WebView in

2019-07-18 12:48发布

I need to play a video from daylimotion in an Android WebView, I've tried with several approuches but haven't found a working solution. The video I need to play is like the one in the following URL:

http://www.dailymotion.com/video/x1iepl4_blackfish-full-documentary_animals

I would appreciate the HTML for the WebView to load the video working with daylimotion or any other approach. I've already done the same for yputube videos successfully, but that solution doesn't work for dailymotion.

Thanks in advance.

3条回答
唯我独甜
2楼-- · 2019-07-18 13:16

Just solved the problem. The solution is as follows:

WebSettings webSettings = this.wvVideo.getSettings();

final String mimeType = "text/html";
final String encoding = "UTF-8";
String html;

webSettings.setJavaScriptEnabled(true);         
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setUserAgentString(null);   
// Taken from the url
String videoId = "x1iepl4_blackfish-full-documentary_animals";
html = this.getHTMLDailyMotion(videoId);            
this.wvVideo.loadDataWithBaseURL("", html, mimeType, encoding, "");

Then the method to build the HTML is as follows:

private String getHTMLDailyMotion(String videoId) {
    String html = "<iframe class=\"youtube-player\" "
            + "style=\"border: 0; width: 100%; height: 95%;"
            + "padding:0px; margin:0px\" "
            + "id=\"ytplayer\" type=\"text/html\" "
            + "src=\"http://www.dailymotion.com/embed/video/" + videoId
            + "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer "
            + "controls onclick=\"this.play()\">\n" + "</iframe>\n";

    return html;
}

This shows the video on full screen.

查看更多
趁早两清
3楼-- · 2019-07-18 13:18

Dailymotion provides a WebView based SDK that includes all the tricks required for you to play the video easily :

dailymotion-sdk-android

The README provides an easy example of integration

查看更多
对你真心纯属浪费
4楼-- · 2019-07-18 13:31
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView wv = (WebView) findViewById(R.id.webview1);
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setAppCacheEnabled(true);
    wv.getSettings().setBuiltInZoomControls(true);
    wv.getSettings().setSaveFormData(true);
    wv.setWebChromeClient(new WebChromeClient());
    wv.setWebViewClient(new Callback());
    wv.loadUrl("http://www.dailymotion.com/video/x1iepl4_blackfish-full-documentary_animals");
}

private class Callback extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return (false);
    }
}
查看更多
登录 后发表回答