android h5视频全屏,【原创】H5播放视频,全屏播放

在工作中,无意遇到一个需求,要在WebView加载H5视频,支持全屏播放。

在Activity中添加需要的参数:

protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

private View customView;

private FrameLayout fullscreenContainer;

private WebChromeClient.CustomViewCallback customViewCallback;

之后给WebSetting设置属性:

WebSettings settings = mWebView.getSettings();

settings.setUseWideViewPort(true);

settings.setJavaScriptEnabled(true);

settings.setAllowFileAccess(true); // 允许访问文件

settings.setBuiltInZoomControls(true);

settings.setSupportZoom(true);

settings.setLoadWithOverviewMode(true);

settings.setCacheMode(WebSettings.LOAD_NO_CACHE);

settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

然后设置WebView点击事件:

mWebView.setWebChromeClient(new WebChromeClient() {

@Override

public void onProgressChanged(WebView view, int newProgress) {

}

@Override

public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {

callback.invoke(origin, true, false);

super.onGeolocationPermissionsShowPrompt(origin, callback);

}

@Override

public View getVideoLoadingProgressView() {

FrameLayout frameLayout = new FrameLayout(MainActivity.this);

frameLayout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

return frameLayout;

}

@Override

public void onShowCustomView(View view, CustomViewCallback callback) {

showCustomView(view, callback);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//播放时横屏幕,如果需要改变横竖屏,只需该参数就行了

}

@Override

public void onHideCustomView() {

hideCustomView();

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//这里设置不播放时,为横屏还是竖屏

}

});

/** 隐藏视频全屏 */

private void hideCustomView() {

if (customView == null) {

return;

}

setStatusBarVisibility(true);

FrameLayout decor = (FrameLayout) getWindow().getDecorView();

decor.removeView(fullscreenContainer);

fullscreenContainer = null;

customView = null;

customViewCallback.onCustomViewHidden();

mWebView.setVisibility(View.VISIBLE);

}

/** 全屏容器界面 */

static class FullscreenHolder extends FrameLayout {

public FullscreenHolder(Context ctx) {

super(ctx);

setBackgroundColor(ctx.getResources().getColor(android.R.color.black));

}

@Override

public boolean onTouchEvent(MotionEvent evt) {

return true;

}

}

private void setStatusBarVisibility(boolean visible) {

int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;

getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);

}

@Override

public boolean onKeyUp(int keyCode, KeyEvent event) {

switch (keyCode) {

case KeyEvent.KEYCODE_BACK:

/** 回退键 事件处理 优先级:视频播放全屏-网页回退-关闭页面 */

if (customView != null) {

hideCustomView();

} else if (mWebView.canGoBack()) {

mWebView.goBack();

} else {

finish();

}

return true;

default:

return super.onKeyUp(keyCode, event);

}

}

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/24537.html

75d087ef9a9fb11dc373caaf33adbf7f.png

微信打赏

支付宝打赏

感谢您对作者Bob的打赏,我们会更加努力!    如果您想成为作者,请点我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值