Skip to content
Merged
Show file tree
Hide file tree
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
@@ -1,4 +1,6 @@
using Android.Media.Session;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Core.Views;
using CommunityToolkit.Maui.DeviceTests.Runners;
using CommunityToolkit.Maui.Views;
using PlayerConsts = AndroidX.Media3.Common.BasePlayer.InterfaceConsts;
Expand Down Expand Up @@ -98,6 +100,59 @@ await WaitUntilAsync(() =>
});
}

// The media notification and the lock screen show the state of the platform media session: while the media plays, its playback speed is the
// Speed of the MediaElement, the rate at which they advance the position they show.
[Fact]
public async Task TheMediaSessionPublishesTheSpeedWhileTheMediaPlays()
{
SetupProbeBuilder();
var mediaElement = await CreateMediaElementAsync(element =>
{
element.ShouldAutoPlay = true;
element.ShouldLoopPlayback = true;
element.Speed = 1.5;
element.Source = MediaSource.FromResource(toneAsset);
});

await HostAsync(mediaElement, async handler =>
{
var controller = CreateSystemMediaController(handler.Manager!);
await WaitUntilAsync(() => controller.PlaybackState is { State: PlaybackStateCode.Playing, PlaybackSpeed: 1.5f }, "The media session never showed the media as playing at a speed of 1.5.");

mediaElement.Speed = 2;

await WaitUntilAsync(() => controller.PlaybackState is { State: PlaybackStateCode.Playing, PlaybackSpeed: 2f }, "The media session never showed the new speed.");
});
}

// While the media is paused, the media session is paused at the position of the player, with a playback speed of 0: the position that the
// media notification and the lock screen show does not advance.
[Fact]
public async Task PauseShowsTheMediaAsPausedInTheMediaSession()
{
SetupProbeBuilder();
var mediaElement = await CreateMediaElementAsync(element =>
{
element.ShouldAutoPlay = true;
element.ShouldLoopPlayback = true;
element.Speed = 1.5;
element.Source = MediaSource.FromResource(toneAsset);
});

await HostAsync(mediaElement, async handler =>
{
var controller = CreateSystemMediaController(handler.Manager!);
await WaitUntilAsync(() => controller.PlaybackState?.State is PlaybackStateCode.Playing, "The media session never showed the media as playing.");

mediaElement.Pause();

await WaitUntilAsync(() => controller.PlaybackState?.State is PlaybackStateCode.Paused, "The media session never showed the media as paused.");
var state = controller.PlaybackState!;
Assert.Equal(0f, state.PlaybackSpeed);
Assert.Equal(GetPlayer(mediaElement).CurrentPosition, state.Position);
});
}

// ExoPlayer clamps a position outside of the media (the last millisecond is the end), so these seeks complete and raise SeekCompleted
// instead of waiting for a ready state that never comes.
[Fact]
Expand Down Expand Up @@ -434,4 +489,8 @@ await HostAsync(mediaElement, async _ =>
// OnPlaybackStateChanged(ready).
static Task<MediaElement> CreateQuietMediaElementAsync() =>
CreateMediaElementAsync(element => element.Source = new UriMediaSource());

// Reads the platform media session of the Media3 session, as the media notification and the lock screen do.
MediaController CreateSystemMediaController(MediaManager manager) =>
new(MauiContext.Context!, GetMediaSession(manager)!.PlatformToken!);
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ await HostAsync(mediaElement, async handler =>
});
}

[Fact]
public async Task PauseShowsTheMediaAsPausedInNowPlaying()
{
// https://github.com/CommunityToolkit/Maui/issues/3383
SetupApple();
var mediaElement = CreateMediaElement();

await HostAsync(mediaElement, async handler =>
{
var player = handler.Manager!.NativePlayer!;
await LoadAsync(mediaElement, handler, ResourceSource(VideoResource));
mediaElement.Play();
await WaitUntilAsync(() => mediaElement.CurrentState is MediaElementState.Playing, "The player did not start playing.");
mediaElement.Speed = 1.5;
await WaitUntilAsync(() => MPNowPlayingInfoCenter.DefaultCenter.NowPlaying?.PlaybackRate is 1.5, "Now Playing did not show the speed.", timeout: 2000);

mediaElement.Pause();
await WaitUntilAsync(() => mediaElement.CurrentState is MediaElementState.Paused && player.Rate is 0, "The player did not pause.");

var nowPlaying = MPNowPlayingInfoCenter.DefaultCenter.NowPlaying!;
Assert.Equal(0f, nowPlaying.PlaybackRate);
Assert.Equal(player.CurrentTime.Seconds, nowPlaying.ElapsedPlaybackTime!.Value, tolerance: 0.1);
Assert.Equal(1.5, mediaElement.Speed);
});
}

[Fact]
public async Task NativeRateChangeOffTheMainThreadIsReflectedInTheMediaElementSpeedOnTheMainThread()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,34 @@ await WithCleanRemoteCommandsAsync(() => InvokeOnMainThreadAsync(() =>
Assert.Equal("Artist", info.Artist);
Assert.Equal(200d, info.PlaybackDuration!.Value, precision: 3);
Assert.Equal(42.5, info.ElapsedPlaybackTime!.Value, precision: 3);
Assert.Equal(1.5f, info.PlaybackRate);
Assert.Equal(0f, info.PlaybackRate); // The player is not playing
Assert.False(info.IsLiveStream);
Assert.Equal(new CGSize(320, 240), info.Artwork!.Bounds.Size);
}));
}

[Fact]
public async Task SetMetadataPublishesTheSpeedAsThePlaybackRateOnlyWhileThePlayerPlays()
{
// https://github.com/CommunityToolkit/Maui/issues/3383
SetupApple();

await WithPlayerAsync(async (player, metadata) =>
{
var mediaElement = await InvokeOnMainThreadAsync(() => new MediaElement { MetadataTitle = "Song", Speed = 1.5 });

await InvokeOnMainThreadAsync(player.Play);
await WaitUntilAsync(() => player.TimeControlStatus is AVPlayerTimeControlStatus.Playing, "The player did not start playing.");
await InvokeOnMainThreadAsync(() => metadata.SetMetadata(player.CurrentItem, mediaElement));
Assert.Equal(1.5f, MPNowPlayingInfoCenter.DefaultCenter.NowPlaying!.PlaybackRate);

await InvokeOnMainThreadAsync(player.Pause);
await WaitUntilAsync(() => player.TimeControlStatus is AVPlayerTimeControlStatus.Paused, "The player did not pause.");
await InvokeOnMainThreadAsync(() => metadata.SetMetadata(player.CurrentItem, mediaElement));
Assert.Equal(0f, MPNowPlayingInfoCenter.DefaultCenter.NowPlaying!.PlaybackRate);
});
}

[Fact]
public async Task SetMetadataWithoutPlayerItemPublishesNoDurationAndNoElapsedTime()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Media;
using Windows.Media.Control;
using Windows.Media.Playback;

namespace CommunityToolkit.Maui.DeviceTests;
Expand Down Expand Up @@ -374,6 +375,93 @@ public async Task OpenedMediaWithMetadataAppearsAsASystemMediaSession()
Assert.Equal(token, (await WinMediaMediaSessions.Bounded(session.TryGetMediaPropertiesAsync())).Title);
}

// While the media plays, its system media session plays at the Speed of the MediaElement, the rate at which the system media overlay advances
// the position it shows. Playing takes a machine that renders the video.
[Fact]
public async Task TheSystemMediaSessionPublishesTheSpeedWhileTheMediaPlays()
{
SetupWindowsBuilder();
await RequireVideoRenderingAsync();
var playback = await PlayInASystemMediaSessionAsync(speed: 1.5);
await WaitForSystemMediaSessionAsync(playback, info => IsPlayingAt(info, 1.5), "The system media session never showed the media as playing at a speed of 1.5.");

await InvokeOnMainThreadAsync(() => playback.View.Speed = 2);

await WaitForSystemMediaSessionAsync(playback, info => IsPlayingAt(info, 2), "The system media session never showed the new speed.");
}

// While the media is paused, its system media session is paused: the system media overlay and the lock screen show the media as paused, at a
// position that does not advance. The session keeps the playback rate of the player, which a pause doesn't change on Windows. Where the video
// doesn't render (the Windows CI jobs), Play() leaves the player paused, and the session shows the paused media the same way.
[Fact]
public async Task PauseShowsTheMediaAsPausedInTheSystemMediaSession()
{
SetupWindowsBuilder();
var playback = await PlayInASystemMediaSessionAsync(speed: 1.5);

await InvokeOnMainThreadAsync(playback.View.Pause);

await WaitForSystemMediaSessionAsync(playback,
info => info.PlaybackStatus is GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused && info.PlaybackRate is double rate && Math.Abs(rate - 1.5) < 0.001,
"The system media session never showed the media as paused at the playback rate of the player.");
}

static bool IsPlayingAt(GlobalSystemMediaTransportControlsSessionPlaybackInfo info, double speed) =>
info.PlaybackStatus is GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing && info.PlaybackRate is double rate && Math.Abs(rate - speed) < 0.001;

// A failure reports what the session showed, also through the session found again, and what the player told its system media transport controls.
static async Task WaitForSystemMediaSessionAsync(SystemMediaSessionPlayback playback, Func<GlobalSystemMediaTransportControlsSessionPlaybackInfo, bool> condition, string message)
{
var deadline = DateTime.UtcNow.AddSeconds(15);
GlobalSystemMediaTransportControlsSessionPlaybackInfo info;
while (!condition(info = playback.Session.GetPlaybackInfo()))
{
if (DateTime.UtcNow > deadline)
{
var foundAgain = (await WinMediaMediaSessions.FindAsync(playback.Token))?.GetPlaybackInfo();
var player = await InvokeOnMainThreadAsync(() =>
{
var controls = playback.Player.SystemMediaTransportControls;
return $"{playback.View.CurrentState}, its player {playback.Player.PlaybackSession.PlaybackState} at a rate of {playback.Player.PlaybackSession.PlaybackRate}, "
+ $"its system media transport controls {controls.PlaybackStatus} at a rate of {controls.PlaybackRate} (enabled: {controls.IsEnabled}, command manager enabled: {playback.Player.CommandManager.IsEnabled})";
});
Assert.Fail($"{message} The session showed {Describe(info)}, and {(foundAgain is null ? "was not found again" : $"{Describe(foundAgain)} when found again")}. The MediaElement was {player}.");
}

await Task.Delay(100);
}

static string Describe(GlobalSystemMediaTransportControlsSessionPlaybackInfo info) => $"{info.PlaybackStatus} at a rate of {info.PlaybackRate?.ToString() ?? "none"}";
}

// A MediaElement with metadata that plays in the configuration the toolkit leaves its player in, as in OpenedMediaWithMetadataAppearsAsASystemMediaSession.
async Task<SystemMediaSessionPlayback> PlayInASystemMediaSessionAsync(double speed)
{
await RequireDecoderAsync();
await RequireMediaSessionAsync();
var token = $"test-{Guid.NewGuid():N}";
var view = NewMediaElement(view =>
{
view.ShouldLoopPlayback = true;
view.MetadataTitle = token;
view.MetadataArtist = token;
});
var handler = await ConnectAsync(view);
await OpenAsync(view, MediaSource.FromFile(WinMediaAssets.VideoPath));

await InvokeOnMainThreadAsync(() =>
{
view.Speed = speed;
view.Play();
});

var session = await WinMediaMediaSessions.FindAsync(token, timeoutMilliseconds: 10000);
Assert.True(session is not null, "This machine shows the system media session of a plain MediaPlayer, but none for the playing MediaElement whose metadata the toolkit published.");
return new(view, await GetPlayerAsync(handler), session, token);
}

sealed record SystemMediaSessionPlayback(MediaElement View, MediaPlayer Player, GlobalSystemMediaTransportControlsSession Session, string Token);

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void SetMetadata(AVPlayerItem? playerItem, IMediaElement? mediaElement)
NowPlayingInfo.Artist = mediaElement.MetadataArtist;
NowPlayingInfo.PlaybackDuration = playerItem?.Duration.Seconds ?? 0;
NowPlayingInfo.IsLiveStream = false;
NowPlayingInfo.PlaybackRate = mediaElement.Speed;
NowPlayingInfo.PlaybackRate = GetPlaybackRate(mediaElement.Speed);
NowPlayingInfo.ElapsedPlaybackTime = playerItem?.CurrentTime.Seconds ?? 0;
NowPlayingInfo.Artwork = new(boundsSize: new(320, 240), requestHandler: _ => GetImage(url));
PublishNowPlayingInfo();
Expand All @@ -105,6 +105,15 @@ public void PublishNowPlayingInfo()
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = NowPlayingInfo;
}

public void PublishPlaybackRate(double speed)
{
NowPlayingInfo.PlaybackRate = GetPlaybackRate(speed);
PublishNowPlayingInfo();
}

// A rate of 0 is how Now Playing shows paused media: the system advances the elapsed time on the lock screen and in Control Center at this rate
double GetPlaybackRate(double speed) => player.TimeControlStatus is AVPlayerTimeControlStatus.Playing ? speed : 0;

/// <summary>
/// Unregisters from the shared <see cref="MPRemoteCommandCenter"/>, which would otherwise keep this instance alive and keep
/// sending it commands for a player that is gone.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@
return asset.TracksWithMediaType(AVMediaTypes.Video.GetConstant() ?? "0").FirstOrDefault();
}

var tracks = await asset.LoadTracksWithMediaTypeAsync(AVMediaTypes.Video.GetConstant() ?? "0").WaitAsync(token);

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (xcode-27)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (xcode-27)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (xcode-27)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (xcode-27)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Sample App using Latest .NET SDK (windows-latest)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Sample App using Latest .NET SDK (windows-latest)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Device Tests / Device Tests (iOS app for older iOS versions)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (windows-latest)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (windows-latest)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (windows-latest)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Library (windows-latest)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

Check warning on line 689 in src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs

View workflow job for this annotation

GitHub Actions / Build Sample App using Latest .NET SDK (xcode-27)

'AVAsset.LoadTracksWithMediaTypeAsync(string)' is obsolete: 'Call 'LoadTracksWithMediaType (..., AVAssetLoadTracksCallback)' instead (or the 'LoadTracksWithMediaType2Async' method).'

return tracks.Count <= 0 ? null : tracks[0];
}
Expand Down Expand Up @@ -1066,11 +1066,7 @@
MediaElement.Speed = player.Rate;
}

if (metaData is not null)
{
metaData.NowPlayingInfo.PlaybackRate = (float)MediaElement.Speed;
metaData.PublishNowPlayingInfo();
}
metaData?.PublishPlaybackRate(MediaElement.Speed);
}

void PlayAtSpeed()
Expand Down
Loading