webview中api大于19后不支持优酷等视频全屏,有什么解决办法吗?即WebChromeClient里面的onShowCustomView方法不能调用。但是微信 uc浏览器都可以正常全屏,自己写的webview在4.4跟5.0.1系统中就不能全屏了。求解~~~困烦ing
1条回答 默认 最新
你知我知皆知 2024-08-04 11:51关注以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
这个问题是由于Android 4.4和5.0的版本对WebChromeClient的API限制有关。
在Android 4.4及更高版本中,WebChromeClient的
onShowCustomView()方法被禁用了,这意味着用户无法通过点击一个按钮或链接来显示自定义视图。如果你想要在这些版本中实现全屏播放,你可能需要使用一些第三方库,例如WebViewFlipper或者VLCWebBrowser,它们可以提供一种方式让用户在全屏模式下观看网页内容。
以下是一个简单的例子:
public class FullScreenVideoPlayer extends WebView { private static final int REQUEST_CODE = 123; private ImageView imageView; public FullScreenVideoPlayer(Context context) { super(context); this.setDrawingCacheEnabled(true); this.getSettings().setJavaScriptCanOpenWindowsAutomatically(false); this.getSettings().setLoadsImagesAutomatically(true); this.getSettings().setAllowFileAccess(true); this.getSettings().setAllowFileAccessFromFileURLs(true); this.getSettings().setAllowUniversalAccessFromFileURLs(true); this.getSettings().setSupportZoom(true); this.getSettings().setBuiltInZoomControls(true); this.getSettings().setDisplayZoomControls(false); this.loadUrl("file:///android_asset/your_video.mp4"); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int desiredHeight = MeasureSpec.getSize(heightMeasureSpec); if (desiredHeight > 0 && desiredHeight < this.getHeight()) { this.measure(MeasureSpec.makeMeasureSpec(this.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(desiredHeight, MeasureSpec.EXACTLY)); } } @Override public boolean onTouchEvent(MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: // Handle touch events here. break; case MotionEvent.ACTION_UP: // Handle touch events here. break; default: return super.onTouchEvent(event); } return true; } @Override public boolean onShowCustomView(View customView, CustomViewCallback callback) { // Call the view's show() method to display it. if (!customView.isShown()) { customView.show(); // Here you can add code to handle the custom view and its actions. // For example, you might want to call customView.pause() or stop() // when the user wants to pause playback. // Note that the custom view is shown by calling show(), so no need to set any visibility. } return true; } @Override public void onHiddenChanged(boolean hidden) { // Called when the view has been made visible again. if (hidden) { // Do something here when the view is shown. // For example, resume playback. } else { // Do something here when the view is hidden. // For example, pause playback. } super.onHiddenChanged(hidden); } }这个类实现了WebView的功能,并且在显示时会隐藏自己的滚动条,这样就可以实现全屏播放了。
解决 无用评论 打赏 举报