Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public partial class MediaManager : Java.Lang.Object, IPlayerListener
readonly SemaphoreSlim seekToSemaphoreSlim = new(1, 1);
bool isAndroidForegroundServiceEnabled = false;

// Ensures MediaOpened is raised once per source; ExoPlayer re-enters the ready state after every seek and rebuffer
bool hasMediaOpened;

// True from the moment a new source is assigned until it is handed to ExoPlayer.
// Until then the player still reports the previous source, which must not overwrite the Opening state or raise events
bool isSourceChanging;

double? previousSpeed;
float volumeBeforeMute = 1;

Expand Down Expand Up @@ -104,6 +111,11 @@ public void OnPlayerStateChanged(bool playWhenReady, int playbackState)
return;
}

if (isSourceChanging)
{
return;
}

// ExoPlayer reports Media3's Player.STATE_* values (idleState to endedState), not android.media.session.PlaybackState values: the
// PlaybackState arms only map the values that are not Media3 states.
var newState = playbackState switch
Expand Down Expand Up @@ -212,7 +224,7 @@ or PlaybackState.StateSkippingToPrevious
/// </remarks>
public void OnPlaybackStateChanged(int playbackState)
{
if (MediaElement.Source is null)
if (isSourceChanging || Player is null || MediaElement.Source is null)
{
return;
}
Expand All @@ -228,6 +240,22 @@ public void OnPlaybackStateChanged(int playbackState)
MediaElement.MediaEnded();
break;
case readyState:
// The deprecated OnPlayerStateChanged has no defined order relative to this callback: derive what MediaOpened
// handlers observe here too, so they see the new state, duration and position
newState = Player.PlayWhenReady
? MediaElementState.Playing
: MediaElementState.Paused;
MediaElement.Duration = TimeSpan.FromMilliseconds(Player.Duration < 0 ? 0 : Player.Duration);
MediaElement.Position = TimeSpan.FromMilliseconds(Player.CurrentPosition < 0 ? 0 : Player.CurrentPosition);
MediaElement.CurrentStateChanged(newState);

// Prepare() only queues the media: the source is open when the player first reaches the ready state for it
if (!hasMediaOpened)
{
hasMediaOpened = true;
MediaElement.MediaOpened();
}

seekToTaskCompletionSource?.TrySetResult();
break;
}
Expand Down Expand Up @@ -529,6 +557,8 @@ protected virtual async partial ValueTask PlatformUpdateSource()

if (MediaElement.Source is null)
{
isSourceChanging = false;

Player.ClearMediaItems();
MediaElement.Duration = TimeSpan.Zero;
MediaElement.CurrentStateChanged(MediaElementState.None);
Expand All @@ -555,6 +585,8 @@ protected virtual async partial ValueTask PlatformUpdateSource()
return;
}

isSourceChanging = true;

Player.PlayWhenReady = MediaElement.ShouldAutoPlay;

// Only a failure to apply the source (to create its media item or media source, or to hand it to the player) is a failure of the source.
Expand All @@ -577,6 +609,8 @@ protected virtual async partial ValueTask PlatformUpdateSource()
item = result?.Build();
if (item?.MediaMetadata is not null)
{
hasMediaOpened = false;

IMediaSource? mediaSource = null;

// If we have a custom stream data source, we need to set the media source differently
Expand All @@ -597,6 +631,8 @@ protected virtual async partial ValueTask PlatformUpdateSource()
// The player raises its events synchronously while it takes the source (StateChanged for Buffering, for example): from here on, only
// an exception of the player itself, a Java exception, is a failure of the source
isPlayerTakingTheSource = true;
isSourceChanging = false;

if (mediaSource is not null)
{
Player.SetMediaSource(mediaSource);
Expand Down Expand Up @@ -626,6 +662,8 @@ protected virtual async partial ValueTask PlatformUpdateSource()
currentStreamDataSourceFactory?.Dispose();
currentStreamDataSourceFactory = null;

isSourceChanging = false;

MediaElement.MediaFailed(new MediaFailedEventArgs(exception.Message));
Logger.LogError(exception, "{LogMessage}", exception.Message);

Expand All @@ -651,12 +689,9 @@ protected virtual async partial ValueTask PlatformUpdateSource()
currentStreamDataSourceFactory?.Dispose();
currentStreamDataSourceFactory = null;

return;
}
isSourceChanging = false;

if (Player.PlayerError is null)
{
MediaElement.MediaOpened();
return;
}

if (isAndroidForegroundServiceEnabled)
Expand Down
Loading