要解决Android WebView视频播放没有图像的问题,但在浏览器中正常播放音频的问题,可以尝试以下方法:
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// 检查是否已经有全屏视图
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
mCustomViewCallback = callback;
mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
mCustomViewContainer.addView(mCustomView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mCustomViewContainer.setVisibility(View.VISIBLE);
}
});
@Override
public void onBackPressed() {
if (mCustomView != null) {
onHideCustomView();
} else if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private void onHideCustomView() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().getDecorView().setSystemUiVisibility(mOriginalSystemUiVisibility);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
}
请确保在AndroidManifest.xml文件中添加了Internet权限:
这些方法应该能够解决Android WebView视频播放没有图像的问题。如果问题仍然存在,可能需要检查视频源是否适配WebView,并确保WebView版本较新。