Fix media opened bug#3252
Open
ne0rrmatrix wants to merge 5 commits into
Open
Conversation
Split OnPlayerStateChanged into OnPlayWhenReadyChanged and OnPlaybackStateChanged for better alignment with ExoPlayer events. Added hasMediaOpened flag to ensure MediaOpened triggers only once. Improved state mapping, event triggering, and notification updates. Cleaned up obsolete methods and followed .NET MAUI Toolkit guidelines.
Update playback state check in OnPlaybackStateChanged Changed the condition to trigger duration and position updates from checking for Playing state to checking if playbackState equals readyState. This ensures media properties are updated at the correct playback lifecycle event.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses Android MediaElement event/state timing by moving MediaOpened to align with ExoPlayer’s STATE_READY transition and refining when playback-state-related updates occur.
Changes:
- Trigger
MediaOpenedfromOnPlaybackStateChanged(STATE_READY)with a guard to prevent duplicate firing. - Add
OnPlayWhenReadyChangedhandling to reflect play/pause transitions after media is opened. - Avoid unconditional notification updates in
PlatformUpdateSource, only updating when the Android foreground service is enabled.
Comment on lines
+408
to
+410
| if (hasSetSource && isAndroidForegroundServiceEnabled) | ||
| { | ||
| if (Player.PlayerError is null) | ||
| { | ||
| MediaElement.MediaOpened(); | ||
| } | ||
|
|
||
| if (isAndroidForegroundServiceEnabled) | ||
| { | ||
| UpdateNotifications(); | ||
| } | ||
| UpdateNotifications(); |
Comment on lines
+140
to
+157
| public void OnPlayWhenReadyChanged(bool playWhenReady, int reason) | ||
| { | ||
| if(!hasMediaOpened) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (playWhenReady) | ||
| { | ||
| MediaElement.CurrentStateChanged(MediaElementState.Playing); | ||
|
|
||
| } | ||
|
|
||
| else | ||
| { | ||
| MediaElement.CurrentStateChanged(MediaElementState.Paused); | ||
| } | ||
| } |
Comment on lines
+169
to
+174
| if (MediaElement.Source is null || Player is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| MediaElementState newState = MediaElement.CurrentState; | ||
| switch (playbackState) | ||
| MediaElementState newState = playbackState switch |
Improve guard conditions in OnPlayWhenReadyChanged to check for nulls and playback state. Simplify state update logic with a ternary operator. Update idleState mapping in OnPlaybackStateChanged to return Stopped instead of None when appropriate.
Set hasMediaOpened to false at the start of PlatformUpdateSource() to ensure the flag accurately reflects the state when a new media source is set. This helps prevent incorrect media state handling during source changes.
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
Linked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal)mainat time of PRAdditional information
This pull request refactors the Android
MediaManagerimplementation for theMediaElementcomponent, focusing on improving how playback state changes are handled and when media events are triggered. The changes streamline state management, ensure accurate notification of media events, and update the interface to better align with ExoPlayer's event model.Playback State Handling Improvements:
OnPlayerStateChangedimplementation and introducing a newOnPlaybackStateChangedmethod that uses a switch expression for clarity and correctness. This ensures thatMediaOpenedis triggered only once when playback starts, andMediaEndedis called when playback stops.OnPlayWhenReadyChangedimplementation to update theMediaElementstate toPlayingorPausedbased on the player's readiness, ensuring state transitions are handled more accurately.Code Cleanup and Interface Updates:
OnPlayWhenReadyChangedmethod and added an emptyOnPlayerStateChangedmethod to maintain interface compliance withIPlayerListener.idleState) and ahasMediaOpenedflag to track whether the media has been opened, preventing duplicate event triggers.Notification and Event Triggering Adjustments:
PlatformUpdateSourceto only callUpdateNotificationswhen source is set and the Android foreground service is enabled, removing the previous unconditional triggering ofMediaOpened.