Allow HTML5 video autoplay inside AgentWeb (closes #339) - #1085
Conversation
There was a problem hiding this comment.
Pull request overview
Enables HTML5 video autoplay in AgentWeb by updating the default WebSettings configuration, addressing issue #339 where video.play() was blocked due to missing gesture exemptions.
Changes:
- Adds
WebSettings.setMediaPlaybackRequiresUserGesture(false)to the default settings pipeline. - Documents the motivation and context for the autoplay setting change inline.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // "play() can only be initiated by a user gesture" because the default | ||
| // gesture requirement was never lifted. Pages that auto-start video | ||
| // playback (most short-form video sites) need this off. | ||
| mWebSettings.setMediaPlaybackRequiresUserGesture(false); |
There was a problem hiding this comment.
WebSettings#setMediaPlaybackRequiresUserGesture was added in API 17. Since this module’s minSdkVersion is 14, calling it unconditionally can crash on API 14–16 with NoSuchMethodError. Please guard this call with a Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 check (or equivalent) so older devices skip it safely.
| mWebSettings.setMediaPlaybackRequiresUserGesture(false); | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
| mWebSettings.setMediaPlaybackRequiresUserGesture(false); | |
| } |
|
感谢贡献,但这个改动暂不合入,说明一下原因。 1. 缺少 API 版本守卫(会导致崩溃)
而 2. 更根本的问题:这是在替所有使用者改默认行为 这行代码会让每一个接入 AgentWeb 的 App 的 WebView 都开始允许自动播放(含声音)。这个副作用对流量、电量和用户体验的影响不小,不适合作为库的默认值。 而使用者现在就已经可以自行开启,不需要改库: public IAgentWebSettings getSettings() {
return new AbsAgentWebSettings() {
@Override
public IAgentWebSettings toSetting(WebView webView) {
IAgentWebSettings settings = super.toSetting(webView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
settings.getWebSettings().setMediaPlaybackRequiresUserGesture(false);
}
return settings;
}
@Override protected void bindAgentWebSupport(AgentWeb agentWeb) { }
};
}如果希望做成库的一等能力,欢迎重开一个 PR,把它做成 另外本 PR 与 #1085 内容重复(同一改动的两个位置),一并说明。 |
Let HTML5 video autoplay inside AgentWeb
Closes #339
What changed
agentweb-core/src/main/java/com/just/agentweb/AbsAgentWebSettings.javaCall
WebSettings.setMediaPlaybackRequiresUserGesture(false)from the default settings pipeline. Without this, everyvideo.play()triggered by JavaScript on the loaded page was rejected by Chromium with the well-known message "play() can only be initiated by a user gesture", which is exactly what the reporter saw on the douyin reflow page.The existing call to
setMixedContentMode(MIXED_CONTENT_ALWAYS_ALLOW)already handles the "Mixed Content" half of the same trace. With both flags in place the same page now plays back normally.Key snippet
Notes
The flag is documented as having no effect for Web Audio in some Chromium versions. That is acceptable here because the issue is about HTML5
<video>playback. Apps that want to keep the Chromium default can overrideAgentWebSettingsImpl.toSettingand re-enable the gesture requirement.