Recent project development has used the function of WebView play video, summed up the mistakes in development, these errors in the development is and easy to encounter, so I summed up here, I hope you see not making similar mistakes, as far as possible to improve the development efficiency:
This demo I also refer to the online write a better demo, after summing up the changes, written out.
The following code is appropriate:
Mainactivity:
Package Com.androidwebviewdemo;import Android.app.activity;import Android.app.progressdialog;import Android.content.pm.activityinfo;import Android.os.bundle;import Android.util.log;import android.view.KeyEvent; Import Android.view.layoutinflater;import Android.view.view;import Android.view.window;import Android.view.windowmanager;import Android.webkit.webchromeclient;import Android.webkit.webchromeclient.customviewcallback;import Android.webkit.websettings;import Android.webkit.webview;import Android.webkit.webviewclient;import android.widget.framelayout;/** * Where to play video with WebView: * 1, add network access (and other required permissions); * 2, Webviewclient method shouldoverrideurlloading can be used to achieve click WebView page link; * 3, WebView play video needs to add webview.setwebchromeclient (new Webchromeclient ()); * 4, Video vertical screen, click Full screen, want to switch to the horizontal screen full screen status, The android:configchanges= "Orientation|screensize" statement must be added to the activity's * configuration file in the Manifest.xml configuration file. * 5, if the video does not play, or play the comparison card, you can use hardware acceleration, that is, in the application, or in the activity of the configuration file add * android:hardwareaccelerated= "true". * @author Zhongyao */publiC Class Mainactivity extends Activity {private WebView webview;private framelayout video_fullview;//full screen video load Viewprivate V Iew xcustomview;private ProgressDialog waitdialog = null;private customviewcallback xCustomViewCallback;private Mywebchromeclient xwebchromeclient; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); requestwindowfeature (window.feature_no_title);//Remove the app title GetWindow (). SetFlags ( Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen); SetContentView ( R.layout.activity_main); waitdialog = new ProgressDialog (this); Waitdialog.settitle ("hint"); Waitdialog.setmessage (" Video page load ... "); Waitdialog.setindeterminate (true); Waitdialog.setcancelable (true); Waitdialog.show (); WebView = ( WebView) Findviewbyid (r.id.webview); Video_fullview = (framelayout) Findviewbyid (R.id.video_fullview); WebSettings ws = Webview.getsettings (); Ws.setbuiltinzoomcontrols (true);//Hide Zoom button//Ws.setlayoutalgorithm ( WebSettings.LayoutAlgorithm.NORMAL);//typesetting adapts to screen ws.setusewideviewport (true);//Can scale Ws.setloadwithoverviewmode (true) arbitrarily;// The Setusewideviewport method sets the window recommended for use by WebView. The Setloadwithoverviewmode method is to set the mode of WebView loaded pages. Ws.setsavepassword (True); Ws.setsaveformdata (true);//Save form Data ws.setjavascriptenabled (TRUE); Ws.setgeolocationenabled (TRUE);//Enable geo-positioning Ws.setgeolocationdatabasepath ("/data/data/org.itri.html5webview/ Databases/");//Set the location of the database path ws.setdomstorageenabled (true); Ws.setsupportmultiplewindows (true);//New plus Xwebchromeclient = new Mywebchromeclient (); webview.setwebchromeclient (xwebchromeclient); Webview.setwebviewclient (new Mywebviewclient ()); Webview.loadurl ("http://look.appjx.cn/mobile_api.php?mod=news&id=12604");} public class Mywebviewclient extends Webviewclient {@Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return false;} @Overridepublic void onpagefinished (WebView view, String URL) {super.onpagefinished (view, URL); Waitdialog.dismiss ();}} public class Mywebchromeclient extends Webchromeclient {private View xprogressvideo;//the full screen will be called when playing network video @overridepublic void Onshowcustomview (view view, Customviewcallback Callback) {setrequestedorientation (activityinfo.screen_orientation_landscape); Webview.setvisibility ( view.invisible);//If a view already exists, terminate immediately and create a new if (xcustomview! = null) {Callback.oncustomviewhidden (); return;} Video_fullview.addview (view); Xcustomview = View;xcustomviewcallback = Callback;video_fullview.setvisibility ( view.visible);} Video playback exits full screen will be called @overridepublic void Onhidecustomview () {if (Xcustomview = = null)//Not full-screen playback status return; Setrequestedorientation (activityinfo.screen_orientation_portrait); xcustomview.setvisibility (View.GONE); video_ Fullview.removeview (xcustomview); Xcustomview = Null;video_fullview.setvisibility (View.gone); Xcustomviewcallback.oncustomviewhidden (); webview.setvisibility (view.visible);} Video loading process loading@overridepublic View Getvideoloadingprogressview () {if (Xprogressvideo = = null) {Layoutinflater Inflater = Layoutinflater.from (mainactivity.this); xprogressvideo = inflater.inflate (r.layout.video_loading_progress, null);} return xprogressvideo;}} /** * Determines if it is full screen * * @return */public boolean incustomview () {return (Xcustomview! = null);} /** * Full screen when pressing the return key execution exit full screen method */public void Hidecustomview () {Xwebchromeclient.onhidecustomview (); Setrequestedorientation ( activityinfo.screen_orientation_portrait);} @Overrideprotected void Onresume () {super.onresume (); Super.onresume (); Webview.onresume (); Webview.resumetimers (); * * Set to horizontal screen */if (getrequestedorientation ()! = Activityinfo.screen_orientation_landscape) {setrequestedorientation ( activityinfo.screen_orientation_portrait);}} @Overrideprotected void OnPause () {super.onpause (); Webview.onpause (); Webview.pausetimers ();} @Overrideprotected void OnDestroy () {Super.ondestroy (); Super.ondestroy (); Video_fullview.removeallviews (); Webview.loadurl ("About:blank"); webview.stoploading (); webview.setwebchromeclient (null); webview.setwebviewclient (null); Webview.destroy (); webView = null;} @Overridepublic boolean onKeyDown (int keycode, KeYevent event) {if (keycode = = Keyevent.keycode_back) {if (Incustomview ()) {//Webviewdetails.loadurl ("About:blank"); Hidecustomview (); return true;} else {webview.loadurl ("About:blank"); MainActivity.this.finish ();}} return false;}}
Activity_main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " android:orientation= "vertical" > <framelayout android:id= "@+id/video_fullview" android:layout _width= "Fill_parent" android:layout_height= "fill_parent" android:visibility= "Gone" > </ framelayout> <webview android:id= "@+id/webview" android:layout_width= "Wrap_content " android:layout_height= "Wrap_content" android:layout_margintop= "20SP"/></linearlayout>
Video_loading_progress.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/progress_indicator " android:layout_width=" wrap_content " android:layout_ height= "Wrap_content" android:layout_centerinparent= "true" android:orientation= "vertical" > < ProgressBar android:id= "@android: id/progress" style= "? Android:attr/progressbarstylelarge" android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:layout_gravity= "center" android:paddingtop= "5dip" android:text= "is loading video ... " android:textcolor="? android:attr/textcolorprimary " android:textsize=" 14SP "/></linearlayout >
Androidmanifest.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.androidwebviewdemo "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" android.p Ermission. INTERNET "/> <uses-permission android:name=" Android.permission.ACCESS_COARSE_LOCATION "/> <uses-permiss Ion Android:name= "Android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name= " Android.permission.ACCESS_MOCK_LOCATION "/> <uses-permission android:name=" Android.permission.ACCESS_GPS "/ > <uses-permission android:name= "Android.permission.ACCESS_ASSISTED_GPS"/> <uses-permission android:na Me= "Android.permission.ACCESS_LOCATION"/><uses-permission android:name= "android.permission.READ_PHONE_ State "/> <application android:allowbackup=" true " Android:hardwareaccelerated= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@style/apptheme" > <!--android:configchanges= "Orientation|keyboardhidden"- <!--The default vertical screen, click the full screen and then horizontal screen, then the activity must be configured android:configchanges= "Orientation|screensize" so that, rotating the screen, will only call Onconfiguration Changed, no new activity is created. Otherwise, the code in the setting of the horizontal screen, will create a new activity, so there is no way to achieve a click on the horizontal screen. --<activity android:name= "Com.androidwebviewdemo.MainActivity" android:configchanges= "Orientation|screensize" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.catego Ry. LAUNCHER "/> </intent-filter> </activity> </application></manifest>
The source code can be downloaded from my uploaded resources.
Android WebView play video (including full screen playback)