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();
       }

以及在清單中加上

    <activity android:name=".activity.WebActivity"
               android:hardwareAccelerated="true"   
               android:configChanges="orientation|keyboardHidden|screenSize"/>

不開硬件加速,視頻會黑屏,有聲音沒圖像;

不設置configChanges,當轉屏以及轉屏造成的屏幕尺寸變化的時候,activity會沖走onCreate方法,前面干的所有事兒都白干了~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容