Skip to content

Fix media opened bug - #3252

Open
ne0rrmatrix wants to merge 6 commits into
CommunityToolkit:mainfrom
ne0rrmatrix:FixMediaOpenedBug
Open

ne0rrmatrix wants to merge 6 commits into
CommunityToolkit:mainfrom
ne0rrmatrix:FixMediaOpenedBug

Conversation

@ne0rrmatrix

@ne0rrmatrix ne0rrmatrix commented Jun 24, 2026 •

Copy link
Copy Markdown
Member

Description of Change

On Android, MediaOpened was raised from PlatformUpdateSource() immediately after Player.Prepare(), before ExoPlayer had actually opened the media. This PR raises it when ExoPlayer first reaches STATE_READY for the current source, and reworks how CurrentState is derived so the two listener callbacks cannot disagree.

Only MediaManager.android.cs changes.

Events

  • MediaOpened is raised from OnPlaybackStateChanged(STATE_READY), once per source. ExoPlayer re-enters STATE_READY after every seek and rebuffer, so a hasMediaOpened flag guards the event. The flag is reset when the new media item is handed to the player.
  • Duration and Position are set, and CurrentState is updated, before MediaOpened is raised, so a handler sees Playing or Paused and a valid duration.
  • MediaEnded is still raised from STATE_ENDED only.

State

  • A single UpdateCurrentState() maps the player's playback state, PlayWhenReady and PlayerError to a MediaElementState:

    ExoPlayer MediaElementState
    STATE_IDLE Failed if the player has an error, otherwise Stopped
    STATE_BUFFERING Buffering
    STATE_READY Playing if PlayWhenReady, otherwise Paused
    STATE_ENDED Stopped
  • OnPlaybackStateChanged and OnPlayWhenReadyChanged both call it. OnPlayWhenReadyChanged only acts in STATE_READY, so it cannot overwrite Opening, Buffering or Stopped.

  • The deprecated OnPlayerStateChanged(bool, int) is removed, along with the PlaybackState constants it switched on. Those were Android media-session values, not ExoPlayer playback states, as described in the linked issue.

Source replacement

  • Until the new item is handed to ExoPlayer (artwork is downloaded first), the player still reports the previous source. The listener is ignored during that window, so Opening is not overwritten and no events are raised for the old media. PlayWhenReady is now applied inside that window, just before the new item is set, so a paused source is no longer resumed while its replacement loads.
  • If Source changes again while an update is awaiting, the older update returns without touching the player.

Behaviour changes to be aware of

  • MediaOpened now 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 reports Stopped.

Linked Issues

PR Checklist

Additional 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.

Scenario Result
Load source Opening → Buffering → Playing, then one MediaOpened with the duration set
Pause / play repeatedly State toggles Paused / Playing on every tap
Seek Paused → Buffering → Paused → Playing, SeekCompleted, no second MediaOpened
Stop, then play Stopped, then Buffering → Playing, no second MediaOpened
Play to the end Stopped and MediaEnded
Invalid URL Buffering → Failed and MediaFailed; state stays Failed
Replace a paused source with an autoplaying one Paused → Opening → Buffering → Playing; Opening is not overwritten by the old item
Change source while playing Opening → Buffering → Playing, one MediaOpened for the new source
Network lost mid-playback Failed and MediaFailed; play after the network returns resumes without a second MediaOpened

Not exercised: a physical device, the foreground service / notification path, and StreamMediaSource.

iOS and Mac Catalyst raise MediaOpened in the same premature way (straight after the player item is replaced). That is not changed here.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings June 24, 2026 14:42

This comment was marked as outdated.

@dotnet-policy-service dotnet-policy-service Bot added stale The author has not responded in over 30 days help wanted This proposal has been approved and is ready to be implemented labels Jul 24, 2026
@ne0rrmatrix
ne0rrmatrix requested a balanced review from Copilot October 1, 2026 22:34

This comment was marked as outdated.

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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted This proposal has been approved and is ready to be implemented stale The author has not responded in over 30 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Android MediaElement.MediaOpened is raised from Player.Prepare() instead of ExoPlayer STATE_READY, leading to unpredictable state transitions

3 participants