fix(AbsAgentWebSettings): allow media playback without user gesture (#339)#1082
Open
jim-daf wants to merge 1 commit intoJustson:androidxfrom
Open
fix(AbsAgentWebSettings): allow media playback without user gesture (#339)#1082jim-daf wants to merge 1 commit intoJustson:androidxfrom
jim-daf wants to merge 1 commit intoJustson:androidxfrom
Conversation
…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.
36a69e4 to
ef400fa
Compare
There was a problem hiding this comment.
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 inAbsAgentWebSettings.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); |
There was a problem hiding this comment.
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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
AbsAgentWebSettings.settings()callssetMixedContentMode(MIXED_CONTENT_ALWAYS_ALLOW)on Lollipop+, so HTTPmedia on an HTTPS page is no longer blocked.
WebSettings.setMediaPlaybackRequiresUserGestureistrue. AgentWebnever overrides it, so embedded players that start playback
programmatically (the douyin player in the report, TikTok-style share
pages, Twitter video, etc.) hit
NotAllowedErrorexactly as quoted.Fix
agentweb-core/src/main/java/com/just/agentweb/AbsAgentWebSettings.java: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.