/**
* 添加直播组件
*/
@SuppressLint("JavascriptInterface")
private void addPlayerLive(final SubjectActivityModule.SubjectActivityBean bean) {
playerLiveBean = bean.getPlayerLive();
View view = mInflater.inflate(R.layout.layout_subject_player, null);
playerWebView = view.findViewById(R.id.wv_survey);
String url = "file:///android_asset/apps/H5FF48005/www/view/artical/iframe.html";
// String url = playerLiveBean.getPlaySrc();
ImageView iv_default = view.findViewById(R.id.iv_default);
WebChromeClient wvcc = new WebChromeClient();
WebSettings webSettings = playerWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
playerWebView.addJavascriptInterface(this, "android");
webSettings.setUseWideViewPort(true); // 关键点
webSettings.setAllowFileAccess(true); // 允许访问文件
webSettings.setSupportZoom(true); // 支持缩放
webSettings.setDomStorageEnabled(true); //启用或禁用DOM缓存。
webSettings.setLoadWithOverviewMode(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容
playerWebView.setVisibility(View.INVISIBLE);
playerWebView.setWebChromeClient(wvcc);
WebViewClient wvc = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
playerWebView.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
iv_default.setVisibility(View.GONE);
playerWebView.setVisibility(View.VISIBLE);
}
};
playerWebView.setWebViewClient(wvc);
playerWebView.setWebChromeClient(new WebChromeClient() {
/*** 视频播放相关的方法 **/
@Override
public View getVideoLoadingProgressView() {
FrameLayout frameLayout = new FrameLayout(SubjectActivityDetailsActivity.this);
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return frameLayout;
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
showCustomView(view, callback);
}
@Override
public void onHideCustomView() {
hideCustomView();
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
// if (newProgress==100){
// iv_default.setVisibility(View.GONE);
// playerWebView.setVisibility(View.VISIBLE);
// }
}
});
// 加载Web地址
playerWebView.loadUrl(url);
ll_container.addView(view);
setPlayerWebViewHeight(view);
}
private void setPlayerWebViewHeight(View view) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (UIUtils.getScreenWidth(this) * 0.5625));
view.setLayoutParams(layoutParams);
}
/**
* 视频播放全屏
**/
private void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (customView != null) {
callback.onCustomViewHidden();
return;
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().getDecorView();
FrameLayout decor = (FrameLayout) getWindow().getDecorView();
fullscreenContainer = new FullscreenHolder(this);
fullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
decor.addView(fullscreenContainer, COVER_SCREEN_PARAMS);
customView = view;
setStatusBarVisibility(false);
customViewCallback = callback;
}
/**
* 隐藏视频全屏
*/
private void hideCustomView() {
if (customView == null || playerWebView == null) {
return;
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setStatusBarVisibility(true);
FrameLayout decor = (FrameLayout) getWindow().getDecorView();
decor.removeView(fullscreenContainer);
fullscreenContainer = null;
customView = null;
customViewCallback.onCustomViewHidden();
playerWebView.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 (playerWebView != null && playerWebView.canGoBack()) {
playerWebView.goBack();
} else {
finish();
}
return true;
default:
return super.onKeyUp(keyCode, event);
}
}
/**
* 供h5调用 返给h5视频播放地址
*/
@JavascriptInterface
public String getPlayerLiveSrc() {
if (playerLiveBean == null || TextUtils.isEmpty(playerLiveBean.getPlaySrc())) {
return "";
}
return playerLiveBean.getPlaySrc();
}