From 810390fe32a1793965b1205642d1f29463fca65b Mon Sep 17 00:00:00 2001 From: Etienne Charland Date: Sun, 25 Jun 2023 21:30:21 -0500 Subject: [PATCH] Load media as async --- Avalonia.Mpv/MpvPlayerHost.cs | 15 ++++++++------- Avalonia.Sample/MainWindow.axaml | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Avalonia.Mpv/MpvPlayerHost.cs b/Avalonia.Mpv/MpvPlayerHost.cs index d342211..d952b4b 100644 --- a/Avalonia.Mpv/MpvPlayerHost.cs +++ b/Avalonia.Mpv/MpvPlayerHost.cs @@ -64,7 +64,7 @@ protected override void SourceChanged(string? value) if (!string.IsNullOrEmpty(value)) { Status = PlaybackStatus.Loading; - LoadMedia(); + var _ = LoadMediaAsync(); } else { @@ -105,13 +105,14 @@ protected override async void OnLoaded() Player.TimePos.Changed += Player_PositionChanged; - Player.Volume.Set(base.Volume); - Player.Speed.Set(base.GetSpeed()); - Player.LoopFile.Set(base.Loop ? "yes" : "no"); + var options = new MpvAsyncOptions { WaitForResponse = false }; + await Player.Volume.SetAsync(base.Volume, options); + await Player.Speed.SetAsync(base.GetSpeed(), options); + await Player.LoopFile.SetAsync(base.Loop ? "yes" : "no", options); if (!string.IsNullOrEmpty(Source) && !_initLoaded) { - LoadMedia(); + await LoadMediaAsync(); } } @@ -197,7 +198,7 @@ protected override void LoopChanged(bool value) Player?.LoopFile.Set(value ? "yes" : "no"); } - private void LoadMedia() + private async Task LoadMediaAsync() { if (!IsLoaded || Design.IsDesignMode) { return; } @@ -207,7 +208,7 @@ private void LoadMedia() _initLoaded = true; Thread.Sleep(10); Player.Pause.Set(!base.AutoPlay); - Player.LoadFile(Source!).Invoke(); + await Player.LoadFile(Source!).InvokeAsync(); } } diff --git a/Avalonia.Sample/MainWindow.axaml b/Avalonia.Sample/MainWindow.axaml index c279ec2..464e860 100644 --- a/Avalonia.Sample/MainWindow.axaml +++ b/Avalonia.Sample/MainWindow.axaml @@ -22,7 +22,7 @@ - +