我正在尝试通过网址在webview上显示youtube视频。经过一个小时的工作,但仍未显示任何内容: -
图片
正如你在上面看到的图像没有显示任何东西但是寻找视频增加的条目(但没有显示任何东西,我使用 API 17和480 * 800(WVGA))以下是我的代码: -
代码
movie_image_movie_link.setOnClickListener(new OnClickListener() {
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(act);
// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.show_video_from_youtube);
dialog.setTitle("YouTube Video");
WebView video = (WebView) dialog.findViewById(R.id.webview);
video.getSettings().setJavaScriptEnabled(true);
video.getSettings().setPluginState(WebSettings.PluginState.ON);
// video.getSettings().setUserAgent(0);
video.setWebChromeClient(new WebChromeClient() {
});
String id = StringUtils.substringBetween(
MovieJSONObjectHandle.youtube_link,
"www.youtube.com/watch?v=", "&");
System.out.println("url => "
+ Uri.parse("http://www.youtube.com/v/" + id)
.toString());
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML(id);
video.loadDataWithBaseURL("", html, mimeType,
encoding, "");
dialog.show();
}
});
public static String getHTML(String videoId) {
String html = "<iframe class=\"youtube-player\" "
+ "style=\"border: 0; width: 100%; height: 90%;"
+ "padding:0px; margin:0namepx\" "
+ "id=\"ytplayer\" type=\"text/html\" "
+ "src=\"http://www.youtube.com/embed/" + videoId
+ "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer "
+ "controls onclick=\"this.play()\">\n" + "</iframe>\n";
/**
* <iframe id="ytplayer" type="text/html" width="640" height="360"
* src="https://www.youtube.com/embed/WM5HccvYYQg" frameborder="0"
* allowfullscreen>
**/
return html;
}
也尝试使用视频代替iframe,但仍然存在同样的问题: -
String html = "<video id=\"video\" width=\"320\" height=\"240\" src=\"http://www.youtube.com/embed/" + videoId
+ "autobuffer controls onclick=\"this.play();\">";
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="400dp"
android:gravity="center"
android:orientation="vertical" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
</LinearLayout>
mainfest
<activity
android:name="biz.xicom.defindme.controlpage.ControlPage"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar" >
</activity>
所以,如果您过去遇到过这个问题,那么请告诉我在上面的代码中我做了什么错误。
答案 0 :(得分:0)
试试这个:
webView = (WebView) findViewById(R.id.webView);
mWebViewClient = new myWebViewClient();
webView.setWebViewClient(mWebViewClient);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSaveFormData(true);
webView.loadUrl("http://m.youtube.com"); // your web url