Skip to content

Allow HTML5 video autoplay inside AgentWeb (closes #339) - #1085

Closed
jim-daf wants to merge 1 commit into
Justson:androidxfrom
jim-daf:fix/media-playback-no-gesture-issue-339
Closed

Allow HTML5 video autoplay inside AgentWeb (closes #339)#1085
jim-daf wants to merge 1 commit into
Justson:androidxfrom
jim-daf:fix/media-playback-no-gesture-issue-339

Conversation

@jim-daf

@jim-daf jim-daf commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Let HTML5 video autoplay inside AgentWeb

Closes #339

What changed

agentweb-core/src/main/java/com/just/agentweb/AbsAgentWebSettings.java

Call WebSettings.setMediaPlaybackRequiresUserGesture(false) from the default settings pipeline. Without this, every video.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

mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
mWebSettings.setMediaPlaybackRequiresUserGesture(false);

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 override AgentWebSettingsImpl.toSetting and re-enable the gesture requirement.

@jim-daf
jim-daf marked this pull request as ready for review April 23, 2026 09:47
Copilot AI review requested due to automatic review settings April 23, 2026 09:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copilot AI Apr 23, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
mWebSettings.setMediaPlaybackRequiresUserGesture(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mWebSettings.setMediaPlaybackRequiresUserGesture(false);
}

Copilot uses AI. Check for mistakes.
@Justson

Justson commented Aug 27, 2026

Copy link
Copy Markdown
Owner

感谢贡献,但这个改动暂不合入,说明一下原因。

1. 缺少 API 版本守卫(会导致崩溃)

WebSettings.setMediaPlaybackRequiresUserGesture()API 17 才引入的:

<method name="setMediaPlaybackRequiresUserGesture(Z)V" since="17"/>

agentweb-coreminSdkVersion14AbsAgentWebSettings#settings() 里对每一处受版本限制的调用都严格加了守卫(JELLY_BEAN / KITKAT / LOLLIPOP),唯独新增的这行没有,在 API 14~16 上会抛 NoSuchMethodError。项目 lint 配置了 abortOnError false,构建不会拦住它。

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,把它做成 AgentWeb.Builder 上的显式开关(默认保持关闭)并补上版本守卫,那样我很乐意合入。issue #339 暂时保持 open。

另外本 PR 与 #1085 内容重复(同一改动的两个位置),一并说明。

@Justson Justson closed this Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

webview播放视频失败

3 participants