關於Android HTML5 audio autoplay無效問題的解決方案_Android

來源:互聯網
上載者:User

前言:在android HTML5 開發中有不少人遇到過 audio 標籤 autoplay在某些裝置上無效的問題,網上大多是講怎麼在js中操作,即在特定的時刻調用audio的play()方法,在android上還是無效。

一、解決方案

在android 4.2添加了允許使用者手勢觸發音視頻播放介面,該介面預設為 true ,即預設不允許自動播放音視頻,只能是使用者互動的方式由使用者自己促發播放。

WebView webView = this.finishActivity(R.id.main_act_webview);// ... ...// 其他配置// ... ...// 設定4.2以後版本支援autoPlay,非使用者手勢促發if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {webView.getSettings().setMediaPlaybackRequiresUserGesture(false);}

通過以上配置就可以載入帶有自動播放的音視頻啦!

二、 源碼分析

下面我們沿著該問題來窺探下WebView的系統源碼:

1、 通過getSettings()擷取到的WebView的配置

/*** Gets the WebSettings object used to control the settings for this* WebView.** @return a WebSettings object that can be used to control this WebView's* settings*/public WebSettings getSettings() {checkThread();return mProvider.getSettings();}

這裡通過一個 mProvider來擷取的配置資訊,通過看WebView的源碼,我們可以看到,WebView的所有操作都是交給 mProvider來進行的。

2、 mPeovider是在哪初始化的?

/*** @hide*/@SuppressWarnings("deprecation") // for super() call into deprecated base class constructor.protected WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes,Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) {super(context, attrs, defStyleAttr, defStyleRes);if (context == null) {throw new IllegalArgumentException("Invalid context argument");}sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion >=Build.VERSION_CODES.JELLY_BEAN_MR2;checkThread();ensureProviderCreated();mProvider.init(javaScriptInterfaces, privateBrowsing);// Post condition of creating a webview is the CookieSyncManager.getInstance() is allowed.CookieSyncManager.setGetInstanceIsAllowed();}

可以看到有個ensureProviderCreated()方法,就是在這裡建立的mProvider:

private void ensureProviderCreated() {checkThread();if (mProvider == null) {// As this can get called during the base class constructor chain, pass the minimum// number of dependencies here; the rest are deferred to init().mProvider = getFactory().createWebView(this, new PrivateAccess());}}

OK,到此知道了mProvider是在WebView的建構函式中建立的,並且WebView的所有操作都是交給mProvider進行的。

3、 但是這個mPeovider到底是誰派來的呢?

看下WebViewFactory#getFactory()做了什麼操作:

static WebViewFactoryProvider getProvider() {synchronized (sProviderLock) {// For now the main purpose of this function (and the factory abstraction) is to keep// us honest and minimize usage of WebView internals when binding the proxy.if (sProviderInstance != null) return sProviderInstance;final int uid = android.os.Process.myUid();if (uid == android.os.Process.ROOT_UID || uid == android.os.Process.SYSTEM_UID) {throw new UnsupportedOperationException("For security reasons, WebView is not allowed in privileged processes");}Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getProvider()");try {Class<WebViewFactoryProvider> providerClass = getProviderClass();StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "providerClass.newInstance()");try {sProviderInstance = providerClass.getConstructor(WebViewDelegate.class).newInstance(new WebViewDelegate());if (DEBUG) Log.v(LOGTAG, "Loaded provider: " + sProviderInstance);return sProviderInstance;} catch (Exception e) {Log.e(LOGTAG, "error instantiating provider", e);throw new AndroidRuntimeException(e);} finally {Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);StrictMode.setThreadPolicy(oldPolicy);}} finally {Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);}}}

可見在23行返回了sProviderInstance, 是由 providerClass 通過反射建立的,15行中通過getProviderClass() 得到了providerClass.

private static Class<WebViewFactoryProvider> getProviderClass() {try {// First fetch the package info so we can log the webview package version.sPackageInfo = fetchPackageInfo();Log.i(LOGTAG, "Loading " + sPackageInfo.packageName + " version " +sPackageInfo.versionName + " (code " + sPackageInfo.versionCode + ")");Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.loadNativeLibrary()");loadNativeLibrary();Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getChromiumProviderClass()");try {return getChromiumProviderClass();} catch (ClassNotFoundException e) {Log.e(LOGTAG, "error loading provider", e);throw new AndroidRuntimeException(e);} finally {Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);}} catch (MissingWebViewPackageException e) {// If the package doesn't exist, then try loading the null WebView instead.// If that succeeds, then this is a device without WebView support; if it fails then// swallow the failure, complain that the real WebView is missing and rethrow the// original exception.try {return (Class<WebViewFactoryProvider>) Class.forName(NULL_WEBVIEW_FACTORY);} catch (ClassNotFoundException e2) {// Ignore.}Log.e(LOGTAG, "Chromium WebView package does not exist", e);throw new AndroidRuntimeException(e);}}

主要的 14行 返回了一個 getChromiumProviderClass(); 是不是有點熟悉,沒錯Android在4.4開始使用強大的Chromium替換掉了原來的WebKit。來看下這個getChromiumProviderClass()。

// throws MissingWebViewPackageExceptionprivate static Class<WebViewFactoryProvider> getChromiumProviderClass()throws ClassNotFoundException {Application initialApplication = AppGlobals.getInitialApplication();try {// Construct a package context to load the Java code into the current app.Context webViewContext = initialApplication.createPackageContext(sPackageInfo.packageName,Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);initialApplication.getAssets().addAssetPath(webViewContext.getApplicationInfo().sourceDir);ClassLoader clazzLoader = webViewContext.getClassLoader();Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "Class.forName()");try {return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY, true,clazzLoader);} finally {Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);}} catch (PackageManager.NameNotFoundException e) {throw new MissingWebViewPackageException(e);}}

最後找到了這個 CHROMIUM_WEBVIEW_FACTORY, 可以看到在 WebViewFactory 中的定義:

private static final String CHROMIUM_WEBVIEW_FACTORY ="com.android.webview.chromium.WebViewChromiumFactoryProvider";

回答2小節的mProvider的初始化,在WebViewChromiumFactoryProvider 的 createWebView(…) 中進行了mProvider的初始化:

@Overridepublic WebViewProvider createWebView(WebView webView, WebView.PrivateAccess privateAccess) {WebViewChromium wvc = new WebViewChromium(this, webView, privateAccess);synchronized (mLock) {if (mWebViewsToStart != null) {mWebViewsToStart.add(new WeakReference<WebViewChromium>(wvc));}}ResourceProvider.registerResources(webView.getContext());return wvc;}

OK,到這裡就真正找到了mProvider 的真正初始化位置,其實它就是一個WebViewChromium,不要忘了我們為什麼費這麼大勁找mProvider,其實是為了分析 webView.getSettings(),這樣就回到了第一小節,通過getSettings()擷取到的WebView的配置。

4、 Settings的初始化

通過第一小節,我們知道Settings是mProvider的一個變數,要想找到Settings就要到 WebViewChromium 來看下:

@Overridepublic WebSettings getSettings() {return mWebSettings;}

接下來就是Settings初始化的地方啦

@Override// BUG=6790250 |javaScriptInterfaces| was only ever used by the obsolete DumpRenderTree// so is ignored. TODO: remove it from WebViewProvider.public void init(final Map<String, Object> javaScriptInterfaces,final boolean privateBrowsing) {if (privateBrowsing) {mFactory.startYourEngines(true);final String msg = "Private browsing is not supported in WebView.";if (mAppTargetSdkVersion >= Build.VERSION_CODES.KITKAT) {throw new IllegalArgumentException(msg);} else {Log.w(TAG, msg);TextView warningLabel = new TextView(mWebView.getContext());warningLabel.setText(mWebView.getContext().getString(com.android.internal.R.string.webviewchromium_private_browsing_warning));mWebView.addView(warningLabel);}}// We will defer real initialization until we know which thread to do it on, unless:// - we are on the main thread already (common case),// - the app is targeting >= JB MR2, in which case checkThread enforces that all usage// comes from a single thread. (Note in JB MR2 this exception was in WebView.java).if (mAppTargetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2) {mFactory.startYourEngines(false);checkThread();} else if (!mFactory.hasStarted()) {if (Looper.myLooper() == Looper.getMainLooper()) {mFactory.startYourEngines(true);}}final boolean isAccessFromFileURLsGrantedByDefault =mAppTargetSdkVersion < Build.VERSION_CODES.JELLY_BEAN;final boolean areLegacyQuirksEnabled =mAppTargetSdkVersion < Build.VERSION_CODES.KITKAT;mContentsClientAdapter = new WebViewContentsClientAdapter(mWebView);mWebSettings = new ContentSettingsAdapter(new AwSettings(mWebView.getContext(), isAccessFromFileURLsGrantedByDefault,areLegacyQuirksEnabled));mRunQueue.addTask(new Runnable() {@Overridepublic void run() {initForReal();if (privateBrowsing) {// Intentionally irreversibly disable the webview instance, so that private// user data cannot leak through misuse of a non-privateBrowing WebView// instance. Can't just null out mAwContents as we never null-check it// before use.destroy();}}});}

在第39行進行了 mWebSettings 的初始化,原來是 ContentSettingsAdapter。

5、 setMediaPlaybackRequiresUserGesture() 分析

經過以上我們隊Google大神的膜拜,我們找到了mWebSettings,下面來看下 setMediaPlaybackRequiresUserGesture方法:

@Overridepublic void setMediaPlaybackRequiresUserGesture(boolean require) {mAwSettings.setMediaPlaybackRequiresUserGesture(require);}

好吧,又是調用的 mAwSettings 的 setMediaPlaybackRequiresUserGesture 方法,那 mAwSettings 是什麼呢?

public ContentSettingsAdapter(AwSettings awSettings) {mAwSettings = awSettings;}

原來是在建構函式中注入的,回到第4小節的最後,這裡 new 了一個AwSettings。

mWebSettings = new ContentSettingsAdapter(new AwSettings(mWebView.getContext(), isAccessFromFileURLsGrantedByDefault,areLegacyQuirksEnabled));

那麼久來 AwSettings 中看下 setMediaPlaybackRequiresUserGesture 吧:

該類位於系統源碼 external/​chromium_org/​android_webview/​java/​src/​org/​chromium/​android_webview/​AwSettings.java

/*** See {@link android.webkit.WebSettings#setMediaPlaybackRequiresUserGesture}.*/public void setMediaPlaybackRequiresUserGesture(boolean require) {synchronized (mAwSettingsLock) {if (mMediaPlaybackRequiresUserGesture != require) {mMediaPlaybackRequiresUserGesture = require;mEventHandler.updateWebkitPreferencesLocked();}}}

可以看到這裡只是給一個變數 mMediaPlaybackRequiresUserGesture 設定了值,然後看到下面一個方法,豁然開朗:

@CalledByNativeprivate boolean getMediaPlaybackRequiresUserGestureLocked() {return mMediaPlaybackRequiresUserGesture;}

該方法是由JNI層調用的,external/​chromium_org/​android_webview/native/aw_settings.cc 中我們看到了:

web_prefs->user_gesture_required_for_media_playback =Java_AwSettings_getMediaPlaybackRequiresUserGestureLocked(env, obj);

可見在核心中去調用該介面,判斷是否允許音視頻的自動播放。

以上所述是小編給大家介紹的關於Android HTML5 audio autoplay無效問題的解決方案,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.