WebView实现视频全屏播放
时间: 2023-11-27 16:02:21 浏览: 72
WebView实现视频全屏播放需要设置WebView的WebChromeClient,重写其中的onShowCustomView和onHideCustomView方法,再在activity的onConfigurationChanged方法中处理屏幕旋转坑点,具体实现方式可以参考以下代码:
//设置WebChromeClient
webView.setWebChromeClient(new WebChromeClient(){
//重写onShowCustomView方法,进入全屏的时候调用
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
//将webView隐藏
webView.setVisibility(View.GONE);
//将视频全屏播放的view添加到activity的根视图中
ViewGroup rootView = getWindow().getDecorView().findViewById(android.R.id.content);
rootView.addView(view);
//横屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
//重写onHideCustomView方法,退出全屏的时候调用
@Override
public void onHideCustomView() {
super.onHideCustomView();
//将视频全屏播放的view从activity的根视图中移除
ViewGroup rootView = getWindow().getDecorView().findViewById(android.R.id.content);
View view = rootView.getChildAt(rootView.getChildCount() - 1);
if (view != null) {
rootView.removeView(view);
}
//将webView显示出来
webView.setVisibility(View.VISIBLE);
//竖屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
//处理屏幕旋转
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//横屏时,隐藏状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
//竖屏时,显示状态栏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
阅读全文