Solution to the invalid problem of Android HTML5 audio autoplay

In the development of Android HTML5, many people have encountered the problem that the audio tag autoplay is invalid on some devices. On the Internet, most of them talk about how to operate in JS, that is, calling the audio play () method at a specific time is still invalid on Android.

1、 Solution

Android 4.2 adds a video playback interface that allows users to gesture and touch pronunciation. By default, this interface is true, that is, automatic playback of audio and video is not allowed by default. It can only be promoted by users through user interaction.

Through the above configuration, you can load audio and video with automatic playback!

2、 Source code analysis

Let's take a peek at the system source code of WebView along this problem:

1. The configuration of WebView obtained through getsettings()

Here, the configuration information is obtained through an mprovider. By looking at the source code of WebView, we can see that all operations of WebView are entrusted to mprovider.

2. Where is mpeovider initialized?

You can see that there is an ensueprovidercreated () method, which is the mprovider created here:

OK, now you know that mprovider is created in the constructor of WebView, and all operations of WebView are entrusted to mprovider.

3. But who sent this mpeovider?

See what webviewfactory #getfactory () does:

It can be seen that sproviderinstance is returned in line 23, which is created by providerclass through reflection. In line 15, providerclass is obtained through getproviderclass()

The main 14 lines return a getchromium providerclass(); Is it a little familiar? Yes, Android began to use the powerful chromium to replace the original WebKit in 4.4. Take a look at this getchromium providerclass ().

Finally found this chromium_ WEBVIEW_ Factory, you can see the definitions in webviewfactory:

Answer the initialization of mprovider in Section 2. Initialize mprovider in createwebview (...) of webviewchromiumfactoryprovider:

OK, here we really find the real initialization location of mprovider. In fact, it is a webviewchromium. Don't forget why we spend so much effort looking for mprovider to analyze WebView Getsettings(), which brings you back to the first section. Get the configuration of WebView through getsettings().

4. Initialization of settings

Through the first section, we know that settings is a variable of mprovider. To find settings, go to webviewchromium:

Next is where settings are initialized

In line 39, the mwebsettings are initialized, which turns out to be the contentsettingsadapter.

5. Setmediaplaybackrequiresusergeture() analysis

After the above worship of our team's Google God, we found mwebsettings. Let's take a look at the setmediaplaybackrequiresusergeture method:

Well, the setmediaplaybackrequiresusergeture method of mawsettings is called again. What is mawsettings?

It was originally injected in the constructor. Back to the end of section 4, here is a new awsettings.

Take a look at setmediaplaybackrequiresusergeture in awsettings for a long time:

This class is located in the system source code external / chromium_ org/android_ webview/java/src/org/chromium/android_ webview/AwSettings. java

You can see that here you just set the value of a variable, mmediaplaybackrequiresusergeture, and then you can see the following method, which suddenly opens up:

This method is called by the JNI layer, external / chromium_ org/android_ webview/native/aw_ settings. In CC, we see:

It can be seen that call the interface in the kernel to judge whether to allow automatic playback of audio and video.

The above is the solution to the invalid problem of Android HTML5 audio autoplay introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>