Android WebView播放视频(包括全屏播放)

转http://blog.csdn.net/u012440207/article/details/39580175

 

最近项目开发中用到了WebView播放视频的功能,总结了开发中犯过的错误,这些错误在开发是及容易遇到的,所以我这里总结了一下,希望大家看到后不要再犯类似的错误,尽可能提高开发效率:

这个Demo我这里也参考了网上写的一个比较好的一个Demo,经过总结修改,写出来的。

以下是相应代码:

MainActivity:

 

[java] view plaincopy
 
  1. package com.androidwebviewdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ProgressDialog;  
  5. import android.content.pm.ActivityInfo;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.KeyEvent;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.Window;  
  12. import android.view.WindowManager;  
  13. import android.webkit.WebChromeClient;  
  14. import android.webkit.WebChromeClient.CustomViewCallback;  
  15. import android.webkit.WebSettings;  
  16. import android.webkit.WebView;  
  17. import android.webkit.WebViewClient;  
  18. import android.widget.FrameLayout;  
  19.   
  20. /** 
  21.  * 使用WebView播放视频时需要注意的地方: 
  22.  * 1、加网络访问权限(及其他所需要的权限);  
  23.  * 2、WebViewClient中方法shouldOverrideUrlLoading可用来实现点击webView页面的链接; 
  24.  * 3、WebView中播放视频需要添加webView.setWebChromeClient(new WebChromeClient()); 
  25.  * 4、视频竖屏时,点击全屏,想要切换到横屏全屏的状态,那么必须在Manifest.xml配置文件该Activity的 
  26.  *  配置文件中添加android:configChanges="orientation|screenSize"语句。 
  27.  * 5、如果视频不能播放,或者播放比较卡,可以采用硬件加速,即在Application,或所在的Activity的配置文件中添加 
  28.  *  android:hardwareAccelerated="true"即可。 
  29.  * @author zhongyao 
  30.  */  
  31. public class MainActivity extends Activity {  
  32.     private WebView webView;  
  33.     private FrameLayout video_fullView;// 全屏时视频加载view  
  34.     private View xCustomView;  
  35.     private ProgressDialog waitdialog = null;  
  36.     private CustomViewCallback xCustomViewCallback;  
  37.     private myWebChromeClient xwebchromeclient;  
  38.   
  39.     @Override  
  40.     protected void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉应用标题  
  43.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  44.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  45.         setContentView(R.layout.activity_main);  
  46.           
  47.         waitdialog = new ProgressDialog(this);  
  48.         waitdialog.setTitle("提示");  
  49.         waitdialog.setMessage("视频页面加载中...");  
  50.         waitdialog.setIndeterminate(true);  
  51.         waitdialog.setCancelable(true);  
  52.         waitdialog.show();  
  53.   
  54.         webView = (WebView) findViewById(R.id.webView);  
  55.         video_fullView = (FrameLayout) findViewById(R.id.video_fullView);  
  56.   
  57.         WebSettings ws = webView.getSettings();  
  58.         ws.setBuiltInZoomControls(true);// 隐藏缩放按钮  
  59.         // ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);// 排版适应屏幕  
  60.   
  61.         ws.setUseWideViewPort(true);// 可任意比例缩放  
  62.         ws.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。  
  63.   
  64.         ws.setSavePassword(true);  
  65.         ws.setSaveFormData(true);// 保存表单数据  
  66.         ws.setJavaScriptEnabled(true);  
  67.         ws.setGeolocationEnabled(true);// 启用地理定位  
  68.         ws.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");// 设置定位的数据库路径  
  69.         ws.setDomStorageEnabled(true);  
  70.         ws.setSupportMultipleWindows(true);// 新加  
  71.         xwebchromeclient = new myWebChromeClient();  
  72.         webView.setWebChromeClient(xwebchromeclient);  
  73.         webView.setWebViewClient(new myWebViewClient());  
  74.         webView.loadUrl("http://look.appjx.cn/mobile_api.php?mod=news&id=12604");  
  75.     }  
  76.   
  77.     public class myWebViewClient extends WebViewClient {  
  78.         @Override  
  79.         public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  80.             view.loadUrl(url);  
  81.             return false;  
  82.         }  
  83.   
  84.         @Override  
  85.         public void onPageFinished(WebView view, String url) {  
  86.             super.onPageFinished(view, url);  
  87.             waitdialog.dismiss();  
  88.         }  
  89.     }  
  90.   
  91.     public class myWebChromeClient extends WebChromeClient {  
  92.         private View xprogressvideo;  
  93.   
  94.         // 播放网络视频时全屏会被调用的方法  
  95.         @Override  
  96.         public void onShowCustomView(View view, CustomViewCallback callback) {  
  97.             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
  98.             webView.setVisibility(View.INVISIBLE);  
  99.             // 如果一个视图已经存在,那么立刻终止并新建一个  
  100.             if (xCustomView != null) {  
  101.                 callback.onCustomViewHidden();  
  102.                 return;  
  103.             }  
  104.             video_fullView.addView(view);  
  105.             xCustomView = view;  
  106.             xCustomViewCallback = callback;  
  107.             video_fullView.setVisibility(View.VISIBLE);  
  108.         }  
  109.   
  110.         // 视频播放退出全屏会被调用的  
  111.         @Override  
  112.         public void onHideCustomView() {  
  113.             if (xCustomView == null)// 不是全屏播放状态  
  114.                 return;  
  115.   
  116.             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
  117.             xCustomView.setVisibility(View.GONE);  
  118.             video_fullView.removeView(xCustomView);  
  119.             xCustomView = null;  
  120.             video_fullView.setVisibility(View.GONE);  
  121.             xCustomViewCallback.onCustomViewHidden();  
  122.             webView.setVisibility(View.VISIBLE);  
  123.         }  
  124.   
  125.         // 视频加载时进程loading  
  126.         @Override  
  127.         public View getVideoLoadingProgressView() {  
  128.             if (xprogressvideo == null) {  
  129.                 LayoutInflater inflater = LayoutInflater  
  130.                         .from(MainActivity.this);  
  131.                 xprogressvideo = inflater.inflate(  
  132.                         R.layout.video_loading_progress, null);  
  133.             }  
  134.             return xprogressvideo;  
  135.         }  
  136.     }  
  137.   
  138.     /** 
  139.      * 判断是否是全屏 
  140.      *  
  141.      * @return 
  142.      */  
  143.     public boolean inCustomView() {  
  144.         return (xCustomView != null);  
  145.     }  
  146.   
  147.     /** 
  148.      * 全屏时按返加键执行退出全屏方法 
  149.      */  
  150.     public void hideCustomView() {  
  151.         xwebchromeclient.onHideCustomView();  
  152.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
  153.     }  
  154.   
  155.     @Override  
  156.     protected void onResume() {  
  157.         super.onResume();  
  158.         super.onResume();  
  159.         webView.onResume();  
  160.         webView.resumeTimers();  
  161.   
  162.         /** 
  163.          * 设置为横屏 
  164.          */  
  165.         if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {  
  166.             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
  167.         }  
  168.     }  
  169.   
  170.     @Override  
  171.     protected void onPause() {  
  172.         super.onPause();  
  173.         webView.onPause();  
  174.         webView.pauseTimers();  
  175.     }  
  176.   
  177.     @Override  
  178.     protected void onDestroy() {  
  179.         super.onDestroy();  
  180.         super.onDestroy();  
  181.         video_fullView.removeAllViews();  
  182.         webView.loadUrl("about:blank");  
  183.         webView.stopLoading();  
  184.         webView.setWebChromeClient(null);  
  185.         webView.setWebViewClient(null);  
  186.         webView.destroy();  
  187.         webView = null;  
  188.     }  
  189.     @Override  
  190.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  191.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  192.             if (inCustomView()) {  
  193.                 // webViewDetails.loadUrl("about:blank");  
  194.                 hideCustomView();  
  195.                 return true;  
  196.             } else {  
  197.                 webView.loadUrl("about:blank");  
  198.                 MainActivity.this.finish();  
  199.             }  
  200.         }  
  201.         return false;  
  202.     }  
  203. }  


activity_main.xml:

 

 

[html] view plaincopy
 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <FrameLayout  
  8.         android:id="@+id/video_fullView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:visibility="gone" >  
  12.     </FrameLayout>  
  13.   
  14.     <WebView  
  15.         android:id="@+id/webView"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_marginTop="20sp" />  
  19.   
  20. </LinearLayout>  


video_loading_progress.xml:

 

 

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/progress_indicator"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:layout_centerInParent="true"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <ProgressBar  
  10.         android:id="@android:id/progress"  
  11.         style="?android:attr/progressBarStyleLarge"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_gravity="center" />  
  15.   
  16.     <TextView  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_gravity="center"  
  20.         android:paddingTop="5dip"  
  21.         android:text="正在玩命加载视频中。。。"  
  22.         android:textColor="?android:attr/textColorPrimary"  
  23.         android:textSize="14sp" />  
  24.   
  25. </LinearLayout>  


AndroidManifest.xml:

 

 

[html] view plaincopy
 
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.androidwebviewdemo"  
    4.     android:versionCode="1"  
    5.     android:versionName="1.0" >  
    6.   
    7.     <uses-sdk  
    8.         android:minSdkVersion="8"  
    9.         android:targetSdkVersion="17" />  
    10.   
    11.     <uses-permission android:name="android.permission.INTERNET" />  
    12.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
    13.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    14.     <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />  
    15.     <uses-permission android:name="android.permission.ACCESS_GPS" />  
    16.     <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />  
    17.     <uses-permission android:name="android.permission.ACCESS_LOCATION" />  
    18.     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>  
    19.     <application  
    20.         android:allowBackup="true"  
    21.         android:hardwareAccelerated="true"  
    22.         android:icon="@drawable/ic_launcher"  
    23.         android:label="@string/app_name"  
    24.         android:theme="@style/AppTheme" >  
    25.   
    26.         <!-- android:configChanges="orientation|keyboardHidden" -->  
    27.         <!-- 默认竖屏,点击全屏后再横屏,  
    28.             那么activity必须配置android:configChanges="orientation|screenSize"  
    29.             这样一来,旋转屏幕,只会调用onConfigurationChanged,不会创建新activity。  
    30.             不然的话,代码中设置横屏的时候,都会新建一个Activity,  
    31.             那样就没办法实现点击就横屏全屏了。 -->  
    32.         <activity  
    33.             android:name="com.androidwebviewdemo.MainActivity"  
    34.             android:configChanges="orientation|screenSize"  
    35.             android:label="@string/app_name" >  
    36.             <intent-filter>  
    37.                 <action android:name="android.intent.action.MAIN" />  
    38.   
    39.                 <category android:name="android.intent.category.LAUNCHER" />  
    40.             </intent-filter>  
    41.         </activity>  
    42.     </application>  
    43.   
    44. </manifest>  

posted on 2015-06-09 17:50  taoboy  阅读(1613)  评论(0)    收藏  举报

导航