Skip to content

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

Open
jim-daf wants to merge 1 commit intoJustson:androidxfrom
jim-daf:fix/media-playback-no-gesture-issue-339
Open

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

Conversation

@jim-daf
Copy link
Copy Markdown

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

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
Copy link
Copy Markdown

Copilot AI left a comment

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

Copilot AI Apr 23, 2026

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.
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播放视频失败

2 participants