Home > Web Front-end > H5 Tutorial > body text

How to make Android WebView support full-screen playback of HTML5 Video (picture)

黄舟
Release: 2017-05-21 15:21:23
Original
3628 people have browsed it

1) Need to be in AndroidManifest.xml It is stated in the file that HardwareAccelerate needs to be used, which can be refined to the Activity level. If the View is not needed, it can be declared not to use acceleration, but it needs to be done in the code, as follows:

a. If you want to declare that the entire application must be accelerated:

         < application
 ... android:hardwareAccelerated ="true">
Copy after login

b. If you want to declare it in Activity, then:

                <activity
 ... android:hardwareAccelerated="true" >,
 还可以更细化到Window, getWindow.setFlags(  WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);                               
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
Copy after login

c. If the application or activity has declared that hardware acceleration is required, but for some reasons (such as power saving?), some Views do not require hardware acceleration,

              view. setLayerType(View.LAYER_TYPE_SOFTWARE, null);

2) It can be said that it is quite strange that you need to use tag, and if it is empty, targetSDK or minSDK must be written, but I have tested it, and it doesn’t matter what version it is written to. . . Theoretically, Android should be able to accelerate 2D rendering starting from 3.0 (API Level 11), but it can be used even if I set the targetSDK to 5, but it still doesn't work without writing this tag.

​ Generally speaking, after the above operations are completed, you can use the video tag to play the video. If you want to support full screen, you need to do a few operations:

1) Give the webview a WebChromeClient object. This WebChromeClient object needs to implement the onShowCustomView and onHideCustomView methods. Here is an implementation example:

    @Override
               public void onShowCustomView(View
 view, CustomViewCallback callback) {
                      if (myCallback != null)
 {
                            myCallback.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;
                     }
               }
Copy after login

Okay, this way of writing is different from many ways of writing on the Internet. Most of the examples on the Internet are that the view received by the onShowCustomView method is a VideoView object, and here it is a VideoView object that cannot be found HTML5## The VideoSurfaceView subclass of #VideoFullScreen, and this subclass is still a private subclass and cannot be accessed from the outside. There is no HTML5VideoFullScreen class in android.jar. If you want to access this class in the application, you need to add the package /platforms//data/layoutlib.jar to the buildpath , and it has only been this way since android-14, that is, after android 4.0. In other words, the method mentioned on the Internet can be used in the android 3.0 era, but not after android 4.0. I use android 4. 0.3.

In fact, no matter what version it is, the general meaning of this code is that in the onShowCustomView method, put the obtained view at the top of the current Activity, and in onHideCustomView, hide the previous view OrDelete, put back the originally covered webview, and end the playback, otherwise it will report to MediaPlayer IllegalStatusException, and it is still a Native method, so there is no way to debug it.

You can download it, use eclipse to import the project, and put the samplevideoPut it in the root directory of sdcard, which is /mnt/sdcard from a system perspective.

The effect is as shown:

Before full screen:


## After full screen:

##

The above is the detailed content of How to make Android WebView support full-screen playback of HTML5 Video (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template