我有在Android上使用WebView播放YouTube的代码。 它适用于新手机(Android 4.4,5.1,6,7 OS),但是当我在Android 4.0.3上试用它时,它会打开带有控件的YouTube框架,点击按钮播放后,灰色背景显示全部时间。
代码:
private void initializeWebView() {
WebView webView = (WebView) findViewById(R.id.webView);
//webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.setBackgroundColor(getResources().getColor(R.color.semi_transparent_black_20percentage));
webView.getSettings().setBuiltInZoomControls(true);
//webView.getSettings().setDisplayZoomControls(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSetting = webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(false);
//webSetting.setLoadWithOverviewMode(true);
//webSetting.setUseWideViewPort(true);
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
webView.setLayoutParams(new RelativeLayout.LayoutParams( screenWidth / 2, LayoutParams.MATCH_PARENT));
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webSetting.setJavaScriptEnabled(true);
webSetting.setBuiltInZoomControls(true);
webSetting.setAllowContentAccess(true);
webSetting.setEnableSmoothTransition(true);
webSetting.setLoadsImagesAutomatically(true);
webSetting.setLoadWithOverviewMode(true);
webSetting.setSupportZoom(false);
webSetting.setUseWideViewPort(true);
webSetting.setAppCacheEnabled(true);
webSetting.setSupportMultipleWindows(true);
webView.setVisibility(View.VISIBLE);
}
public void loadPageIntoWebView(String htmlFilename){
AssetManager mgr = getBaseContext().getAssets();
try {
String htmlContentInStringFormat = "";
WebView webView = (WebView) findViewById(R.id.webView);
String frameVideo = "<body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/2a7f29Jvihc?wmode=transparent\" frameborder=\"0\" allowtransparency></iframe></body>";
htmlContentInStringFormat = frameVideo;
webView.loadDataWithBaseURL(null, htmlContentInStringFormat, "text/html", "utf-8", null);
} catch (Exception e) {
e.printStackTrace();
}
}
此代码运行良好,可在较新的手机上播放YouTube视频(Android OS4.4-7)。
我应该改变什么才能让它在Android 4.0.3上运行?
答案 0 :(得分:0)
解决!
添加以下行后,它适用于Android 4.0.3以及:
webView.setWebChromeClient(new WebChromeClient() {});
这不是强制性的,但我在此行之前添加了该行: webView.setWebViewClient(新的WebViewClient()
希望此解决方案能够帮助其他同事使用Epson Moverio BT-200智能眼镜等。
- )