webview播放视频并且点击全屏横屏的方法


title: webview播放视频并且点击全屏横屏的方法
date: 2016-11-28 10:33:27
tags: tips


  • 首先initWebView,好吧有点多,有些是项目里其他需求需要的,不要在意这些细节···
public static void initWebView(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setUseWideViewPort(true); // 关键点
        settings.setAllowFileAccess(true); // 允许访问文件
        settings.setSupportZoom(true); // 支持缩放
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
//        settings.setPluginState(PluginState.ON);
//        settings.setPluginsEnabled(true);//可以使用插件
//        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setCacheMode(NetUtils.hasNetwork(D8Application.getContext())?WebSettings.LOAD_DEFAULT:WebSettings.LOAD_CACHE_ELSE_NETWORK);  //设置 缓存模式  
        // 开启 DOM storage API 功能  
        settings.setDomStorageEnabled(true);
        //开启 database storage API 功能  
        settings.setDatabaseEnabled(true);
//        String cacheDirPath = getActivity().getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME;
      String cacheDirPath = D8Application.getInstance().getCacheDir().getAbsolutePath()+Constant.APP_DB_CACHE_DIRNAME;  
        Logger.i("cacheDirPath="+cacheDirPath);
        //设置数据库缓存路径  
        webView.getSettings().setDatabasePath(cacheDirPath);
        //设置  Application Caches 缓存目录  
        webView.getSettings().setAppCachePath(cacheDirPath);
        //开启 Application Caches 功能  
        settings.setAppCacheEnabled(true);
    }
  • 设置webviewclient,在当前的webview加载新的url,不设置的话会打开一个浏览器的
web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
}
  • 设置webChromeClient
web.setWebChromeClient(new WebChromeClient() {
            /*** 视频播放相关的方法 **/

            @Override
            public View getVideoLoadingProgressView() {
                FrameLayout frameLayout = new FrameLayout(mContext);
                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);
            }

            @Override
            public void onHideCustomView() {
                hideCustomView();
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
                result.confirm();
                return true;
            }
//            @Override
//            public void onReceivedTitle(WebView view, String title) {
//                super.onReceivedTitle(view, title);
//                CharSequence pnotfound = "404";
//                if (title.contains(pnotfound)) {
//                    view.stopLoading();
//                    rlNoNet.setVisibility(View.VISIBLE);
//                }
//            }

        });
  • 全屏其实就是获取decorView,重新绘制一个全屏的framelayout盖在原有界面上,下面是一些方法
 /**
      * 视频播放全屏
      **/
      
       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;
           }
           setStatusBarVisibility(false);
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
           web.setVisibility(View.INVISIBLE);
           FrameLayout decor = (FrameLayout) getWindow().getDecorView();
           fullscreenContainer = new FullscreenHolder(mContext);
           fullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
           decor.addView(fullscreenContainer, COVER_SCREEN_PARAMS);
           customView = view;
           customViewCallback = callback;
       }
    
       /**
        * 隐藏视频全屏
        */
       private void hideCustomView() {
           if (customView == null) {
               return;
           }
    
           setStatusBarVisibility(true);
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
           FrameLayout decor = (FrameLayout) getWindow().getDecorView();
           decor.removeView(fullscreenContainer);
           fullscreenContainer = null;
           customView = null;
           customViewCallback.onCustomViewHidden();
           web.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 void onBackPressed() {
           /** 回退键 事件处理 优先级:视频播放全屏-网页回退-关闭页面 */
           if (customView != null) {
               hideCustomView();
           } else if (web.canGoBack()) {
               web.goBack();
           } else {
               super.onBackPressed();
           }
       }
  • 最后不要忘了
   @Override
       protected void onDestroy() {
           web.destroy();
           super.onDestroy();
       }

以及在清单中加上

    

不开硬件加速,视频会黑屏,有声音没图像;

不设置configChanges,当转屏以及转屏造成的屏幕尺寸变化的时候,activity会冲走onCreate方法,前面干的所有事儿都白干了~

你可能感兴趣的