BaseWebChromeClient
public class BaseWebChromeClient extends WebChromeClient implements MediaPlayer.OnPreparedListener,
MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
boolean mIsVideoFullscreen = false;
FrameLayout mVideoViewFrameLayout;
CustomViewCallback mVideoViewCallback;
Callback mOutCallback;
WebView mWebView;
View mNonFullscreenVideoLayout;
ViewGroup mFullscreenVideoLayout;
public interface Callback {
void enterFullscreenVideo();
void exitFullscreenVideo();
}
class JavaScriptInterface {
WebChromeClient mWebChromeClient;
public JavaScriptInterface(WebChromeClient webChromeClient) {
mWebChromeClient = webChromeClient;
}
@android.webkit.JavascriptInterface
@SuppressWarnings("unused")
public void notifyVideoEnd() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (mWebChromeClient != null) {
mWebChromeClient.onHideCustomView();
}
}
});
}
}
public void init(WebView webview, View nonFullscreenVideoLayout,
ViewGroup fullScreenVideoLayout,
Callback callback) {
mWebView = webview;
mNonFullscreenVideoLayout = nonFullscreenVideoLayout;
mFullscreenVideoLayout = fullScreenVideoLayout;
mOutCallback = callback;
webview.addJavascriptInterface(new JavaScriptInterface(this),
"_VideoEnabledWebView");
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (view instanceof FrameLayout) {
FrameLayout frameLayout = (FrameLayout) view;
View focusedChild = frameLayout.getFocusedChild();
mIsVideoFullscreen = true;
mVideoViewFrameLayout = frameLayout;
mVideoViewCallback = callback;
mNonFullscreenVideoLayout.setVisibility(View.INVISIBLE);
mFullscreenVideoLayout.addView(mVideoViewFrameLayout, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mFullscreenVideoLayout.setVisibility(View.VISIBLE);
if (focusedChild instanceof android.widget.VideoView) {
VideoView videoView = (VideoView) focusedChild;
videoView.setOnPreparedListener(this);
videoView.setOnCompletionListener(this);
videoView.setOnErrorListener(this);
} else {
if (mWebView != null && mWebView.getSettings().getJavaScriptEnabled()
&& focusedChild instanceof SurfaceView) {
String js = "javascript:";
js += "var _ytrp_html5_video_last;";
js += "var _ytrp_html5_video = document.getElementsByTagName('video')[0];";
js += "if (_ytrp_html5_video != undefined && _ytrp_html5_video != _ytrp_html5_video_last) {";
{
js += "_ytrp_html5_video_last = _ytrp_html5_video;";
js += "function _ytrp_html5_video_ended() {";
{
js += "_VideoEnabledWebView.notifyVideoEnd();";
}
js += "}";
js += "_ytrp_html5_video.addEventListener('ended', _ytrp_html5_video_ended);";
}
js += "}";
injectJS(mWebView, js);
}
}
if (mOutCallback != null) {
mOutCallback.enterFullscreenVideo();
}
}
}
@Override
public void onHideCustomView() {
if (mIsVideoFullscreen) {
mFullscreenVideoLayout.setVisibility(View.INVISIBLE);
mFullscreenVideoLayout.removeView(mVideoViewFrameLayout);
mNonFullscreenVideoLayout.setVisibility(View.VISIBLE);
if (mVideoViewCallback != null
&& !mVideoViewCallback.getClass().getName().contains(".chromium.")) {
mVideoViewCallback.onCustomViewHidden();
}
mIsVideoFullscreen = false;
mVideoViewFrameLayout = null;
mVideoViewCallback = null;
if (mOutCallback != null) {
mOutCallback.exitFullscreenVideo();
}
}
}
private void injectJS(WebView webview, String script) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.evaluateJavascript(script, null);
} else {
webview.loadUrl("javascript:" + script);
}
}
@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
return false;
}
@Override
public void onCompletion(MediaPlayer arg0) {
onHideCustomView();
}
@Override
public void onPrepared(MediaPlayer arg0) {
}
public boolean onBackPressed() {
if (mIsVideoFullscreen) {
onHideCustomView();
return true;
} else {
return false;
}
}
}
页面
mWebChromeClient.init(mWebView, mNonVideoViewContainer, mVideoViewContainer,
new Callback() {
@Override
public void enterFullscreenVideo() {
WindowManager.LayoutParams attrs = getWindow().getAttributes()
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
getWindow().setAttributes(attrs)
if (android.os.Build.VERSION.SDK_INT >= 14) {
// noinspection all
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LOW_PROFILE)
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
}
@Override
public void exitFullscreenVideo() {
WindowManager.LayoutParams attrs = getWindow().getAttributes()
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
getWindow().setAttributes(attrs)
if (android.os.Build.VERSION.SDK_INT >= 14) {
// noinspection all
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_VISIBLE)
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
}
})
@Override
protected void onResume() {
super.onResume();
mWebView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mWebView.onPause();
}
AndroidManifest.xml
activity
android:configChanges="keyboardHidden|orientation|screenSize"