Skip to content

Commit

Permalink
Load media as async
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryx93 committed Jun 26, 2023
1 parent 900676a commit 810390f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions Avalonia.Mpv/MpvPlayerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected override void SourceChanged(string? value)
if (!string.IsNullOrEmpty(value))
{
Status = PlaybackStatus.Loading;
LoadMedia();
var _ = LoadMediaAsync();
}
else
{
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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; }

Expand All @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Avalonia.Sample/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<Grid>
<ui:MediaPlayer x:Name="Player">
<!-- <bass:BassPlayerHost x:Name="PlayerHost" Source="E:\NaturalGrounding\AOA\Like a Cat.mp4" AutoPlay="true" /> -->
<mpv:MpvPlayerHost Source="/run/media/hanuman/Storage-ntfs/NaturalGrounding/AOA/Like a Cat.mp4" Volume="50" Loop="True" AutoPlay="False" />
<mpv:MpvPlayerHost Source="/run/media/hanuman/Storage-ntfs/NaturalGrounding/AOA/Like a Cat.mp4" Volume="50" Loop="True" />
<!-- Source="E:\Music\INNA\Body And The Sun\CD1\07 Fool Me.mp3" Pitch="0.9818181818181818181818" -->
</ui:MediaPlayer>
<!-- <ui:MediaPlayer Height="50" /> -->
Expand Down

0 comments on commit 810390f

Please sign in to comment.