由于项目需求,在进入webview时需要自动播放h5里的video视频,国内网上找了比较多方案都不行,最后还是在Google上找到了解决办法: stackoverflow
加入一行代码:
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
WebView 一大堆的 setting 让人摸不着头脑 ,带着好奇心点开了改方法的源码:
/**
* See {@link android.webkit.WebSettings#setMediaPlaybackRequiresUserGesture}.
*/
public void setMediaPlaybackRequiresUserGesture(boolean require) {
synchronized (mAwSettingsLock) {
if (mMediaPlaybackRequiresUserGesture != require) {
mMediaPlaybackRequiresUserGesture = require;
mEventHandler.updateWebkitPreferencesLocked();
}
}
}
这里只是给变量mMediaPlaybackRequiresUserGesture设值,然后调用:
@CalledByNative
private boolean getMediaPlaybackRequiresUserGestureLocked() {
return mMediaPlaybackRequiresUserGesture;
}
方法由JNI层调用,
web_prefs->user_gesture_required_for_media_playback =
Java_AwSettings_getMediaPlaybackRequiresUserGestureLocked(env, obj);
到这里可以发现,在内核中去调用该接口,判断是否允许自动播放音视频
网友评论