WebView中播放视频,全屏的适配
2018-07-19 本文已影响0人
烂吹笙
WebView中播放视频,全屏的适配
Android中WebView的Bug很多,今天总结下加载WebView中视频,用户点击全屏按钮无法全屏的适配。
正常情况下,用户点击全屏按钮,我们进入一个视频的横屏的全屏状态。
网页点击全屏按钮会触发WebChromeClient的onShowCustomView方法,全屏后缩回来会触发onHideCustomView方法,So我们只需要在这两个方法中处理就行了。
1.我们定义一个接口,接口中有上面两个方法
package com.wj.webviewvideo.webview;
import android.view.View;
import android.webkit.WebChromeClient;
public interface IVideo {
void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback);
void onHideCustomView();
boolean isVideoState();
}
2.接口的实现类,核心!
package com.wj.webviewvideo.webview;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;
import java.util.HashSet;
import java.util.Set;
public class VideoImpl implements IVideo, EventInterceptor {
private Activity mActivity;
private WebView mWebView;
private static final String TAG = VideoImpl.class.getSimpleName();
private Set<Pair<Integer, Integer>> mFlags = null;
private View mMoiveView = null;
private ViewGroup mMoiveParentView = null;
private WebChromeClient.CustomViewCallback mCallback;
public VideoImpl(Activity mActivity, WebView webView) {
this.mActivity = mActivity;
this.mWebView = webView;
mFlags = new HashSet<>();
}
@Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Activity mActivity;
if ((mActivity = this.mActivity) == null || mActivity.isFinishing()) {
return;
}
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Window mWindow = mActivity.getWindow();
Pair<Integer, Integer> mPair = null;
// 保存当前屏幕的状态
if ((mWindow.getAttributes().flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == 0) {
mPair = new Pair<>(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 0);
mWindow.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mFlags.add(mPair);
}
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) && (mWindow.getAttributes().flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) == 0) {
mPair = new Pair<>(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, 0);
mWindow.setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
mFlags.add(mPair);
}
if (mMoiveView != null) {
callback.onCustomViewHidden();
return;
}
if (mWebView != null) {
mWebView.setVisibility(View.GONE);
}
if (mMoiveParentView == null) {
FrameLayout mDecorView = (FrameLayout) mActivity.getWindow().getDecorView();
mMoiveParentView = new FrameLayout(mActivity);
mMoiveParentView.setBackgroundColor(Color.BLACK);
mDecorView.addView(mMoiveParentView);
}
this.mCallback = callback;
mMoiveParentView.addView(this.mMoiveView = view);
mMoiveParentView.setVisibility(View.VISIBLE);
}
@Override
public void onHideCustomView() {
if (mMoiveView == null) {
return;
}
if (mActivity != null && mActivity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
if (!mFlags.isEmpty()) {
for (Pair<Integer, Integer> mPair : mFlags) {
mActivity.getWindow().setFlags(mPair.second, mPair.first);
}
mFlags.clear();
}
mMoiveView.setVisibility(View.GONE);
if (mMoiveParentView != null && mMoiveView != null) {
mMoiveParentView.removeView(mMoiveView);
}
if (mMoiveParentView != null) {
mMoiveParentView.setVisibility(View.GONE);
}
if (this.mCallback != null) {
mCallback.onCustomViewHidden();
}
this.mMoiveView = null;
if (mWebView != null) {
mWebView.setVisibility(View.VISIBLE);
}
}
@Override
public boolean isVideoState() {
return mMoiveView != null;
}
@Override
public boolean event() {
if (isVideoState()) {
onHideCustomView();
return true;
} else {
return false;
}
}
}
EventInterceptor这个是一个接口
package com.wj.webviewvideo.webview;
public interface EventInterceptor {
boolean event();
}
3.封装一个核心BaseWebChromeClient方便以后的WebView的适配
package com.wj.webviewvideo.webview;
import android.view.View;
import android.webkit.WebChromeClient;
public class BaseWebChromeClient extends WebChromeClient {
private IVideo mIVideo;
public BaseWebChromeClient(IVideo mIVideo) {
this.mIVideo = mIVideo;
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (mIVideo != null) {
mIVideo.onShowCustomView(view, callback);
}
}
@Override
public void onHideCustomView() {
if (mIVideo != null) {
mIVideo.onHideCustomView();
}
}
}
4.调用,并且适配一下Acitivity
给你WebView所在Acitivity的清单文件中添加以下属性,
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true"
调用其实很简单给WebView设置WebChromeClient就行了
package com.wj.webviewvideo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.wj.webviewvideo.webview.BaseWebChromeClient;
import com.wj.webviewvideo.webview.VideoImpl;
public class SecondActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
webView = findViewById(R.id.webview);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setUseWideViewPort(true);
webView.setWebChromeClient(new BaseWebChromeClient(new VideoImpl(this, webView)));
webView.loadUrl(MainActivity.WEB_URL);
}
@Override
protected void onResume() {
super.onResume();
if (webView != null)
webView.onResume();
}
@Override
protected void onPause() {
super.onPause();
if (webView != null)
webView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (webView != null)
webView.destroy();
}
}
上面的代码参考的AgentWeb,大致的原理就是全屏时,吧手机横屏WebView放在最上方。