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) {            // A video wants to be shown            FrameLayout frameLayout = (FrameLayout) view;            View focusedChild = frameLayout.getFocusedChild();            // Save video related variables            mIsVideoFullscreen = true;            mVideoViewFrameLayout = frameLayout;            mVideoViewCallback = callback;            // Hide the non-video view, add the video view, and show it            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) {                // android.widget.VideoView (typically API level <11)                VideoView videoView = (VideoView) focusedChild;                // Handle all the required events                videoView.setOnPreparedListener(this);                videoView.setOnCompletionListener(this);                videoView.setOnErrorListener(this);            } else {                // Other classes, including:                // - android.webkit.HTML5VideoFullScreen$VideoSurfaceView,                // which inherits from android.view.SurfaceView (typically                // API level 11-18)                // - android.webkit.HTML5VideoFullScreen$VideoTextureView,                // which inherits from android.view.TextureView (typically                // API level 11-18)                // -                // com.android.org.chromium.content.browser.ContentVideoView$VideoSurfaceView,                // which inherits from android.view.SurfaceView (typically                // API level 19+)                // Handle HTML5 video ended event only if the class is a                // SurfaceView                // Test case: TextureView of Sony Xperia T API level 16                // doesn't work fullscreen when loading the javascript below                if (mWebView != null && mWebView.getSettings().getJavaScriptEnabled()                        && focusedChild instanceof SurfaceView) {                    // Run javascript code that detects the video end and                    // notifies the Javascript interface                    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);                }            }            // Notify full-screen change            if (mOutCallback != null) {                mOutCallback.enterFullscreenVideo();            }        }    }    @Override    public void onHideCustomView() {        // This method should be manually called on video end in all cases        // because it's not always called automatically.        // This method must be manually called on back key press (from this        // class' onBackPressed() method).        if (mIsVideoFullscreen) {            // Hide the video view, remove it, and show the non-video view            mFullscreenVideoLayout.setVisibility(View.INVISIBLE);            mFullscreenVideoLayout.removeView(mVideoViewFrameLayout);            mNonFullscreenVideoLayout.setVisibility(View.VISIBLE);            // Call back (only in API level <19, because in API level 19+ with            // chromium webview it crashes)            if (mVideoViewCallback != null                    && !mVideoViewCallback.getClass().getName().contains(".chromium.")) {                mVideoViewCallback.onCustomViewHidden();            }            // Reset video related variables            mIsVideoFullscreen = false;            mVideoViewFrameLayout = null;            mVideoViewCallback = null;            // Notify full-screen change            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

activityandroid:configChanges="keyboardHidden|orientation|screenSize"

更多相关文章

  1. 安全新手入坑——HTML标签
  2. Nginx系列教程(四)| 一文带你读懂Nginx的动静分离
  3. android自动登陆SharedPreferences
  4. Android架构组件_LiveData
  5. Viewpager2—登录注册引导页面
  6. android 重新加载网络页面设置
  7. 【腾讯Bugly干货分享】Android内存泄漏的简单检查与分析方法
  8. Android(安卓)中Scheme协议的使用详解
  9. android html唤醒APP(原生)

随机推荐

  1. Android(安卓)sdk content loader 0%的解
  2. 又优化了一下Android(安卓)ListView 异步
  3. AutoCAD即将为Android智能手机和平板电脑
  4. JNI和NDK学习(1)--搭建开发环境
  5. Android(安卓)解决android4.0系统中菜单(
  6. android 中vector的用法
  7. 记录WebView从开始加载到渲染结束的时间
  8. 使用Android辅助服务AccessibilityServic
  9. android studio 中aidl文件的使用并且实
  10. H5无法调起android app 的坑之 scheme 大