Skip to content

fix(AbsAgentWebSettings): allow media playback without user gesture (#339)#1082

Open
jim-daf wants to merge 1 commit intoJustson:androidxfrom
jim-daf:fix/security-issue-339
Open

fix(AbsAgentWebSettings): allow media playback without user gesture (#339)#1082
jim-daf wants to merge 1 commit intoJustson:androidxfrom
jim-daf:fix/security-issue-339

Conversation

@jim-daf
Copy link
Copy Markdown

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

Allow programmatic media playback in AgentWeb's WebView (#339)

Resolves #339 - embedded video fails with play() can only be initiated by a user gesture.

Why playback fails

The reporter's logs include two errors:

Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated by a user gesture.
Mixed Content: ... request has been blocked; the content must be served over HTTPS.
  • The mixed-content half is already addressed in current upstream:
    AbsAgentWebSettings.settings() calls
    setMixedContentMode(MIXED_CONTENT_ALWAYS_ALLOW) on Lollipop+, so HTTP
    media on an HTTPS page is no longer blocked.
  • The user-gesture half is not. The platform default for
    WebSettings.setMediaPlaybackRequiresUserGesture is true. AgentWeb
    never overrides it, so embedded players that start playback
    programmatically (the douyin player in the report, TikTok-style share
    pages, Twitter video, etc.) hit NotAllowedError exactly as quoted.

Fix

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

  • In settings(WebView), immediately after the existing JS / zoom /
    password block, add
    mWebSettings.setMediaPlaybackRequiresUserGesture(false);.

This matches what most in-app browser components do (Glide-WebView,
WebView+, etc.) and is what every reproduction in the issue thread asks
for. No other behavior changes.

…ustson#339)

Resolves Justson#339

Issue Justson#339 reports that an embedded video on a page loaded in AgentWeb
fails with:

  Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated
  by a user gesture.

The platform default for setMediaPlaybackRequiresUserGesture is true, and
AbsAgentWebSettings.settings() never overrides it. Many embedded video
players (TikTok/Douyin share pages, Twitter video, etc.) start playback
programmatically and break under that default.

This change calls mWebSettings.setMediaPlaybackRequiresUserGesture(false)
in settings() so the platform's WebView allows programmatic playback,
matching the behavior most in-app browsers want.

The mixed-content half of the same issue (HTTP video on HTTPS page) is
already handled by the existing setMixedContentMode(MIXED_CONTENT_ALWAYS_ALLOW)
call further down in the same method.
@jim-daf jim-daf changed the title fix: block mixed content (3 files) fix(AbsAgentWebSettings): allow media playback without user gesture (#339) Apr 21, 2026
@jim-daf jim-daf force-pushed the fix/security-issue-339 branch from 36a69e4 to ef400fa Compare April 21, 2026 19:59
@jim-daf jim-daf marked this pull request as ready for review April 21, 2026 20:47
Copilot AI review requested due to automatic review settings April 21, 2026 20: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

Updates AgentWeb’s default WebSettings to allow embedded media elements to start playback programmatically (fixing the “play() can only be initiated by a user gesture” failure reported in #339).

Changes:

  • Disable WebSettings’ user-gesture requirement for media playback in AbsAgentWebSettings.settings(WebView).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// Issue #339: HTMLMediaElement.play() throws "API can only be initiated by
// a user gesture" for embedded video players that auto-play. Allow
// programmatic playback so embedded players (e.g. share-page videos) work.
mWebSettings.setMediaPlaybackRequiresUserGesture(false);
Copy link

Copilot AI Apr 21, 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, but this library’s minSdkVersion is 14 (agentweb-core/build.gradle:9). Calling it unconditionally here can crash on API 14–16 (NoSuchMethodError/verification failure). Guard the call with if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) (or use reflection) so pre-17 devices don’t break.

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