我需要在Android WebView中播放来自daylimotion的视频,我尝试了几个approuches但是没有找到可行的解决方案。 我需要播放的视频类似于以下网址中的视频:
http://www.dailymotion.com/video/x1iepl4_blackfish-full-documentary_animals
我很感激WebView的HTML加载使用daylimotion或任何其他方法的视频。我已经成功地为yputube视频做了同样的事情,但该解决方案不适用于dailymotion。
提前致谢。
答案 0 :(得分:8)
答案 1 :(得分:1)
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);
}
}
答案 2 :(得分:1)
刚刚解决了问题。解决方案如下:
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, "");
然后构建HTML的方法如下:
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;
}
这会全屏显示视频。