Repository navigation
Fix media opened bug - #3252
Open
ne0rrmatrix wants to merge 6 commits into
Open
Fix media opened bug#3252ne0rrmatrix wants to merge 6 commits into
ne0rrmatrix wants to merge 6 commits into
Conversation
MediaOpened was raised from PlatformUpdateSource right after Prepare, before the media had actually opened (CommunityToolkit#3248). It is now raised the first time ExoPlayer reaches the ready state for a source, and the player listeners stay gated while a new source is being applied, so the previous source cannot overwrite the Opening state or raise events for it. Fixes CommunityToolkit#3248 Co-Authored-By: Claude Code <noreply@anthropic.com>
TheCodeTraveler
force-pushed
the
FixMediaOpenedBug
branch
from
November 12, 2026 09:24
a2b00ce to
0306989
Compare
Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
This branch has not been deployed
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.
Description of Change
On Android,
MediaOpenedwas raised fromPlatformUpdateSource()immediately afterPlayer.Prepare(), before ExoPlayer had actually opened the media. This PR raises it when ExoPlayer first reachesSTATE_READYfor the current source, and reworks howCurrentStateis derived so the two listener callbacks cannot disagree.Only
MediaManager.android.cschanges.Events
MediaOpenedis raised fromOnPlaybackStateChanged(STATE_READY), once per source. ExoPlayer re-entersSTATE_READYafter every seek and rebuffer, so ahasMediaOpenedflag guards the event. The flag is reset when the new media item is handed to the player.DurationandPositionare set, andCurrentStateis updated, beforeMediaOpenedis raised, so a handler seesPlayingorPausedand a valid duration.MediaEndedis still raised fromSTATE_ENDEDonly.State
A single
UpdateCurrentState()maps the player's playback state,PlayWhenReadyandPlayerErrorto aMediaElementState:STATE_IDLEFailedif the player has an error, otherwiseStoppedSTATE_BUFFERINGBufferingSTATE_READYPlayingifPlayWhenReady, otherwisePausedSTATE_ENDEDStoppedOnPlaybackStateChangedandOnPlayWhenReadyChangedboth call it.OnPlayWhenReadyChangedonly acts inSTATE_READY, so it cannot overwriteOpening,BufferingorStopped.The deprecated
OnPlayerStateChanged(bool, int)is removed, along with thePlaybackStateconstants it switched on. Those were Android media-session values, not ExoPlayer playback states, as described in the linked issue.Source replacement
Openingis not overwritten and no events are raised for the old media.PlayWhenReadyis now applied inside that window, just before the new item is set, so a paused source is no longer resumed while its replacement loads.Sourcechanges again while an update is awaiting, the older update returns without touching the player.Behaviour changes to be aware of
MediaOpenednow arrives later than before: after the media is ready rather than as soon as the source is assigned.Stop()called before the media has ever become ready reportsStopped.Linked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal)mainat time of PRAdditional information
Tests: none added. The change is entirely in Android platform code, which the unit test project (plain
net10.0) does not compile, and the repo has no device test project.Samples: no sample change needed; the existing MediaElement page exercises every path.
Manual testing: run on an Android API 36 emulator using the sample app's MediaElement page, with the page's event handlers temporarily writing to logcat.
Opening→Buffering→Playing, then oneMediaOpenedwith the duration setPaused/Playingon every tapPaused→Buffering→Paused→Playing,SeekCompleted, no secondMediaOpenedStopped, thenBuffering→Playing, no secondMediaOpenedStoppedandMediaEndedBuffering→FailedandMediaFailed; state staysFailedPaused→Opening→Buffering→Playing;Openingis not overwritten by the old itemOpening→Buffering→Playing, oneMediaOpenedfor the new sourceFailedandMediaFailed; play after the network returns resumes without a secondMediaOpenedNot exercised: a physical device, the foreground service / notification path, and
StreamMediaSource.iOS and Mac Catalyst raise
MediaOpenedin the same premature way (straight after the player item is replaced). That is not changed here.🤖 Generated with Claude Code