Home Web Front-end H5 Tutorial How to make Android WebView support full-screen playback of HTML5 Video (picture)

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

May 21, 2017 pm 03:21 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles