Reuse replay controls for live playback

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paul Schneider 2026-11-16 12:12:18 +00:00
commit 2f4a7cb4c9
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
11 changed files with 86 additions and 30 deletions

View file

@ -24,7 +24,8 @@ internal static class PlatformBootstrap
Platform.BackgroundChatService = AndroidBackgroundChatService.Instance;
Platform.ShowSoftKeyboard = MainActivity.ShowSoftKeyboard;
Platform.TryGetCurrentLocationAsync = AndroidCurrentLocationProvider.TryGetCurrentLocationAsync;
Platform.CreateVideoSurface = (player, replay) => new AndroidVideoSurface(player, replay);
Platform.CreateVideoSurface = (player, replay, playbackFailed) =>
new AndroidVideoSurface(player, replay, showControls: true, playbackFailed);
if (System.OperatingSystem.IsAndroidVersionAtLeast(26))
{
Platform.CreateWebRtcMedia = () => System.OperatingSystem.IsAndroidVersionAtLeast(26)

View file

@ -14,7 +14,7 @@ using Button = Android.Widget.Button;
namespace PostIt.Android.Services;
internal sealed class AndroidVideoSurface(MediaPlayer player, bool replay) : NativeControlHost, IDisposable
internal sealed class AndroidVideoSurface(MediaPlayer player, bool replay, bool showControls = false, Action? playbackFailed = null) : NativeControlHost, IDisposable
{
private VideoView? _video;
private ReplayLayout? _layout;
@ -44,9 +44,9 @@ internal sealed class AndroidVideoSurface(MediaPlayer player, bool replay) : Nat
{
if (!_disposed) _ready.TrySetResult();
}, Avalonia.Threading.DispatcherPriority.Loaded);
if (replay && !_disposed)
if ((replay || showControls) && !_disposed)
{
_layout = new ReplayLayout(activity, video, player);
_layout = new ReplayLayout(activity, video, player, replay, playbackFailed);
return new AndroidViewControlHandle(_layout);
}
return new AndroidViewControlHandle(video);
@ -110,19 +110,23 @@ internal sealed class AndroidVideoSurface(MediaPlayer player, bool replay) : Nat
private readonly Button _toggle;
private readonly SeekBar _seek;
private readonly TextView _time;
private readonly Action? _playbackFailed;
private bool _stopped;
public ReplayLayout(Context context, VideoView video, MediaPlayer player) : base(context)
public ReplayLayout(Context context, VideoView video, MediaPlayer player, bool replay, Action? playbackFailed) : base(context)
{
_player = player;
_playbackFailed = playbackFailed;
AddView(video, new FrameLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent));
_bar = new LinearLayout(context) { Orientation = Orientation.Horizontal };
_bar.SetGravity(GravityFlags.CenterVertical);
_bar.SetPadding(12, 8, 12, 8);
_bar.SetBackgroundColor(global::Android.Graphics.Color.Argb(210, 20, 20, 20));
_toggle = new Button(context) { Text = "Pause", ContentDescription = "Lecture ou pause du replay" };
_toggle = new Button(context) { Text = "Pause", ContentDescription = "Lecture ou pause de la vidéo" };
_seek = new SeekBar(context) { Max = 10000, ContentDescription = "Position dans le replay" };
_time = new TextView(context);
_seek.Visibility = replay ? ViewStates.Visible : ViewStates.Gone;
_time.Visibility = replay ? ViewStates.Visible : ViewStates.Gone;
_time.SetTextColor(global::Android.Graphics.Color.White);
_bar.AddView(_toggle);
_bar.AddView(_seek, new LinearLayout.LayoutParams(0, LayoutParams.WrapContent, 1));
@ -160,7 +164,7 @@ internal sealed class AndroidVideoSurface(MediaPlayer player, bool replay) : Nat
private void Toggle(object? sender, EventArgs e)
{
if (_player.IsPlaying) _player.Pause();
else _player.Play();
else if (!_player.Play()) _playbackFailed?.Invoke();
_state.Show();
}

View file

@ -441,7 +441,7 @@ public class BlogReaderTests
var vm = new VideoPageViewModel(new Uri("https://org.example.test/files/paul/clip.mp4"), "video/mp4");
var page = Assert.IsType<VideoPage>(new ViewLocator().Build(vm));
Assert.NotNull(page.FindControl<ContentControl>("VideoSurface"));
Assert.NotNull(page.FindControl<Button>("PlayButton"));
Assert.Null(page.FindControl<Button>("PlayButton"));
}
[AvaloniaFact]
@ -454,7 +454,7 @@ public class BlogReaderTests
var status = page.FindControl<TextBlock>("PlaybackStatus")!;
Platform.SuspendPlayback();
Assert.Equal("Lecture suspendue.", status.Text);
Assert.False(page.FindControl<Button>("PlayButton")!.IsEnabled);
Assert.Null(page.FindControl<ContentControl>("VideoSurface")!.Content);
Platform.ResumePlayback();
Assert.Null(page.FindControl<ContentControl>("VideoSurface")!.Content);
await nav.PopAsync();

View file

@ -4,6 +4,8 @@ using System.Runtime.InteropServices;
using LibVLCSharp.Shared;
using Avalonia.Controls;
using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Interactivity;
using PostIt.Services;
using Yavsc.Api.Client;
@ -81,6 +83,47 @@ public sealed class PodcastReplayTests
finally { window.Close(); }
}, _ct);
[AvaloniaTheory]
[InlineData(true)]
[InlineData(false)]
public void Desktop_controls_show_seeking_only_for_replay(bool replay)
{
var controls = new PostIt.Controls.ReplayControls(replay);
Assert.NotNull(controls.FindControl<Button>("ToggleButton"));
Assert.Equal(replay, controls.FindControl<Slider>("PositionSlider")!.IsVisible);
Assert.Equal(replay, controls.FindControl<TextBlock>("TimeLabel")!.IsVisible);
}
[AvaloniaFact(Skip = "Set POSTIT_TEST_REPLAY to a local MP4 and install LibVLC.", SkipUnless = nameof(HasNativeFixture))]
public async Task Desktop_live_controls_pause_and_resume_the_player()
{
Core.Initialize();
using var vlc = new LibVLC("--vout=dummy", "--aout=dummy", "--no-xlib");
using var player = new MediaPlayer(vlc);
using var media = new Media(vlc, Environment.GetEnvironmentVariable("POSTIT_TEST_REPLAY")!, FromType.FromPath);
media.AddOption(":input-repeat=100");
var controls = new PostIt.Controls.ReplayControls(player, replay: false);
var toggle = controls.FindControl<Button>("ToggleButton")!;
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(_ct);
timeout.CancelAfter(TimeSpan.FromSeconds(15));
async Task WaitForPlayback(bool playing)
{
while (player.IsPlaying != playing)
await Task.Delay(20, timeout.Token);
}
Assert.True(player.Play(media));
try
{
await WaitForPlayback(true);
toggle.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
await WaitForPlayback(false);
toggle.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
await WaitForPlayback(true);
}
finally { player.Stop(); }
}
[Fact]
public async Task Ranges_use_authenticated_factory_with_bounded_exact_content_and_no_token_in_URL()
{

View file

@ -6,7 +6,7 @@
VerticalAlignment="Bottom">
<Grid ColumnDefinitions="Auto,*,Auto" ColumnSpacing="12">
<Button x:Name="ToggleButton" Content="Pause" Click="Toggle_Click"
AutomationProperties.Name="Lecture ou pause du replay" />
AutomationProperties.Name="Lecture ou pause de la vidéo" />
<Slider x:Name="PositionSlider" Grid.Column="1" Minimum="0" Maximum="1"
ValueChanged="Position_Changed" VerticalAlignment="Center"
AutomationProperties.Name="Position dans le replay" />

View file

@ -32,7 +32,17 @@ public partial class ReplayControls : UserControl
};
}
public ReplayControls(MediaPlayer player) : this() => _player = player;
internal ReplayControls(bool replay) : this()
{
PositionSlider.IsVisible = replay;
TimeLabel.IsVisible = replay;
}
public ReplayControls(MediaPlayer player) : this(player, replay: true) { }
public ReplayControls(MediaPlayer player, bool replay) : this(replay) => _player = player;
public event Action? PlaybackFailed;
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
@ -86,7 +96,7 @@ public partial class ReplayControls : UserControl
{
if (_player is not { } player) return;
if (player.IsPlaying) player.Pause();
else player.Play();
else if (!player.Play()) PlaybackFailed?.Invoke();
_state.Show();
}
}

View file

@ -40,7 +40,7 @@ public static class Platform
public static Func<string, CancellationToken, Task<IPodcastCapture>>? CreateBlurredPodcastPreviewAsync { get; set; }
/// <summary>Platform-specific video surface inside the shared video page.</summary>
public static Func<MediaPlayer, bool, Control>? CreateVideoSurface { get; set; }
public static Func<MediaPlayer, bool, Action, Control>? CreateVideoSurface { get; set; }
public static event Action? PlaybackSuspending;
public static event Action? PlaybackResuming;

View file

@ -9,10 +9,7 @@
<Border Grid.Row="1" Background="Black" BorderThickness="1" BorderBrush="Green">
<ContentControl x:Name="VideoSurface" />
</Border>
<StackPanel Grid.Row="2" Orientation="Horizontal" Spacing="12">
<Button x:Name="PlayButton" Content="Lire / Pause" Click="PlayPause_Click" />
<Button Content="Ouvrir dans le navigateur" Click="OpenExternal_Click" IsVisible="{Binding CanOpenExternal}" />
</StackPanel>
<Button Grid.Row="2" Content="Ouvrir dans le navigateur" Click="OpenExternal_Click" IsVisible="{Binding CanOpenExternal}" />
<TextBlock x:Name="PlaybackStatus" Grid.Row="3" TextWrapping="Wrap" />
</Grid>
</ContentPage>

View file

@ -80,11 +80,11 @@ public partial class VideoPage : ContentPage
: new Media(_libVlc, vm.Source);
if (_podcastInput is not null)
media.AddOption(":demux=ts");
var surface = Platform.CreateVideoSurface?.Invoke(_player, replay)
var surface = Platform.CreateVideoSurface?.Invoke(_player, replay, PlaybackResumeFailed)
?? new LibVLCSharp.Avalonia.VideoView
{
MediaPlayer = _player,
Content = replay ? new PostIt.Controls.ReplayControls(_player) : null
Content = CreatePlaybackControls(_player, replay)
};
VideoSurface.Content = surface;
var player = _player;
@ -94,7 +94,6 @@ public partial class VideoPage : ContentPage
if (ReferenceEquals(_player, player) && surface is LibVLCSharp.Avalonia.VideoView desktopVideo)
desktopVideo.TryUpdateNativeControlPosition();
}, DispatcherPriority.Loaded);
PlayButton.IsEnabled = true;
PlaybackStatus.Text = replay ? "video/mp4" : vm.MimeType;
if (!_player.Play(media))
PlaybackStatus.Text = vm.Podcast is null
@ -131,7 +130,6 @@ public partial class VideoPage : ContentPage
_initialization?.Cancel();
_initialization?.Dispose();
_initialization = null;
PlayButton.IsEnabled = false;
_podcastInput?.Cancel();
_replayInput?.Cancel();
if (VideoSurface.Content is LibVLCSharp.Avalonia.VideoView desktopVideo)
@ -183,16 +181,15 @@ public partial class VideoPage : ContentPage
: "Impossible de lire le podcast.";
});
private void PlayPause_Click(object? sender, RoutedEventArgs e)
private PostIt.Controls.ReplayControls CreatePlaybackControls(MediaPlayer player, bool replay)
{
if (_player is null)
return;
if (_player.IsPlaying)
_player.Pause();
else if (!_player.Play())
PlaybackStatus.Text = "Impossible de reprendre la vidéo.";
var controls = new PostIt.Controls.ReplayControls(player, replay);
controls.PlaybackFailed += PlaybackResumeFailed;
return controls;
}
private void PlaybackResumeFailed() => PlaybackStatus.Text = "Impossible de reprendre la vidéo.";
private async void OpenExternal_Click(object? sender, RoutedEventArgs e)
{
if (DataContext is not VideoPageViewModel vm)