我目前正在尝试在我的Android应用中实现嵌入youtube,该应用使用WebView显示其内容,我的目标是API 14及更高版本,但问题是iframe youtube embeds doesn& #39; t在4.2.2及以下版本的webview上显示,但在4.3及以上版本上工作正常。我错过了我错过的内容,但是我在这里找到的代码是我用来实现的代码:
String customHtml = "";
String video; // = "<iframe src=\"http://www.youtube.com/embed/" + "dRvIiIVoVNw" + "\" width=\"100%\" height=\""+ 320 +"px\" frameborder=\"0\"></iframe>";
video = "<div id=\"player\"></div>" +
" <script>" +
" var tag = document.createElement('script');" +
" tag.src = \"https://www.youtube.com/iframe_api\";" +
" var firstScriptTag = document.getElementsByTagName('script')[0];" +
" firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);" +
" var player;" +
" function onYouTubeIframeAPIReady() {" +
" player = new YT.Player('player', {" +
" height: '390'," +
" width: '640'," +
" videoId: 'dRvIiIVoVNw'," +
" events: {" +
" 'onReady': onPlayerReady," +
" 'onStateChange': onPlayerStateChange" +
" }" +
" });" +
" }" +
" function onPlayerReady(event) {" +
" event.target.playVideo();" +
" }" +
" var done = false;" +
" function onPlayerStateChange(event) {" +
" if (event.data == YT.PlayerState.PLAYING && !done) {" +
" setTimeout(stopVideo, 6000);" +
" done = true;" +
" }" +
" }" +
" function stopVideo() {" +
" player.stopVideo();" +
" }" +
" </script>";
customHtml = "<html>" + "<body>"
/*add the video here*/
+ video
+ "<b><font size=\""
+ 5 + "\">"
//+ "<div id='wrap'>"
+ "Test title"
+ "</font></b>"
+ "<font size=\"" + 3 + "\"><br><br><i>Detail1: " + "Test" + "<br/>" + new_date + "<br />Detail2: "+ "Test" +"</i></font><br><br>"
+ "<font size=\"" + 3 + "\">"
+ "Detail content" + "</font>"
+ "<br><br><br></body></html>";
webView.loadData(customHtml, "text/html; charset=utf-8", "UTF-8");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebViewClient(new DetailWebViewClient());
DetailWebViewClient只是一个简单的覆盖:
private class DetailWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("http")) {
Intent webView = new Intent(getBaseContext(), ActivityWebView.class);
webView.putExtra("url", url);
startActivity(webView);
return true;
}
return false;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String callbackUrl) {
super.onReceivedError(view, errorCode, description, callbackUrl);
Toast.makeText(getBaseContext(),
description + " errorcode=" + errorCode, Toast.LENGTH_SHORT)
.show();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
现在我尝试的是iframe和iframe API here,但两者都没有运气。
我也在我的清单上使用android:hardwareAccelerated="true"
,但仍然无法正常工作。我也在其他问题上搜索过同样的问题,但所有这些解决方案都没有在我的最终工作(WebChromeClient,PluginsEnabled,HardwareAccelerated)。希望你能有人帮助我。
答案 0 :(得分:2)
已修复它,问题是以下几行:
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
所以这是我使用的完整webview设置:
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setHorizontalScrollBarEnabled(false);
webView.loadData(customHtml, "text/html; video/mpeg", "UTF-8");
webView.setWebViewClient(new DetailWebViewClient());
你现在在我身边工作得很好。 :)