Android programming enables WebView to support HTML5 video Full-screen playback _android

Source: Internet
Author: User

This example describes the solution to Android programming that enables WebView to support HTML5 video Full-screen playback. Share to everyone for your reference, specific as follows:

1 need to declare in the Androidmanifest.xml file need to use Hardwareaccelerate, can be refined to the activity level, if the unwanted view can be declared do not use acceleration, but need to do in the code, as follows:

A. If you want to declare the entire application to accelerate:

Copy Code code as follows:
<application ... android:hardwareaccelerated = "true" >

B. If you want to declare it in an activity:
Copy Code code as follows:
<activity android:hardwareaccelerated= "true" >
You can also refine to window, Getwindow.setflags (
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

C. If application or activity has stated that hardware acceleration is needed, for some reason (e.g., power saving?) , some view does not require hardware acceleration,

Copy Code code as follows:
View.setlayertype (view.layer_type_software, NULL);

2 It can be said that it is very strange that you need to use the <use-sdk></use-sdk> tag in the Androidmanifest.xml file, and if it is empty, you must write TARGETSDK or MINSDK, But I have tried, no matter how many versions are not related ... In theory, Android should be able to accelerate 2D rendering from 3.0 (API level 11), but I can use the TARGETSDK set to 5, but this tag doesn't work.

In general, the above operation is done, you can use the video tag to play the screen, if you want to support full-screen, you need to do a little action:

1) give WebView a Webchromeclient object, this Webchromeclient object needs to implement Onshowcustomview and Onhidecustomview methods, the following is an implementation example:

@Override public void Onshowcustomview (view view, Customviewcallback callback) {if (mycallback!= null) {mycal
      Lback.oncustomviewhidden ();
      Mycallback = null;
   Return
   Long id = thread.currentthread (). GetId (); Wrtlog.
   V ("Widgetchromeclient", "Rong Debug in Showcustomview Ex:" + ID);
   ViewGroup parent = (ViewGroup) mwebview.getparent ();
   String s = Parent.getclass (). GetName (); Wrtlog.
   V ("Widgetchromeclient", "Rong Debug Ex:" + s);
   Parent.removeview (Mwebview);
   Parent.addview (view);
   MyView = view;
   Mycallback = callback;
Chromeclient = this;
Private View myview = null;
Private Customviewcallback mycallback = null;
   public void Onhidecustomview () {Long id = thread.currentthread (). GetId (); Wrtlog.
   V ("Widgetchromeclient", "Rong Debug in Hidecustom Ex:" + ID);
          if (MyView!= null) {if (mycallback!= null) {Mycallback.oncustomviewhidden ();
      Mycallback = null; } viewgroup parent = (VIEWGroup) myview.getparent ();
      Parent.removeview (MyView);
      Parent.addview (Mwebview);
   MyView = null; }
}

Well, this writing is not the same as a lot of writing on the Internet, most of the online examples are Onshowcustomview method received view is a Videoview object, And here is a videosurfaceview subclass of the html5videofullscreen that is not found, and this subclass is a private subclass, and there is no way to access it outside, There is no html5videofullscreen this class in Android.jar. If you want to access this class in application, you need to put the <android-sdk-path>/platforms/<android-version>/data/ Layoutlib.jar this bag added to the BuildPath, and from the android-14, that is, android4.0 after this, that is, the online said the method in the android3.0 era is available, android4.0 later on, I use the Androi d4.0.3.

In fact, whatever the version, this code probably means that in the Onshowcustomview method, the captured view is placed at the top of the current activity, in the Onhidecustomview, the previous view is hidden or deleted, The original covered WebView put back, and the end of playback, will be reported MediaPlayer illegalstatusexception, and native method, simply can not debug.

The above code is fragment, many friends see also can't solve the problem, complete instance code code clicks here This station downloads.

Can be downloaded, with Eclipse to import the project, the sample video into the SDcard root directory, from the system point of view is/mnt/sdcard can.

Effect as shown:

Before full screen:

Full screen after:

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.