From a4d197efc542094544c78fcd157fe781a542813f Mon Sep 17 00:00:00 2001 From: Tung Huynh <31434093+huynhsontung@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:51:21 -0800 Subject: [PATCH] fix: media not playing when launch from file (#535) * fix: media not playing when launch from file try to play the media when the player is not initialized * a more complete fix --- .../ViewModels/MediaListViewModel.cs | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Screenbox.Core/ViewModels/MediaListViewModel.cs b/Screenbox.Core/ViewModels/MediaListViewModel.cs index a54a761c6..62b83c641 100644 --- a/Screenbox.Core/ViewModels/MediaListViewModel.cs +++ b/Screenbox.Core/ViewModels/MediaListViewModel.cs @@ -133,8 +133,16 @@ public void Receive(MediaPlayerChangedMessage message) { async void SetPlayQueue() { - ClearPlaylist(); - await EnqueueAndPlay(_delayPlay); + if (_delayPlay is MediaViewModel media && Items.Contains(media)) + { + PlaySingle(media); + await TryEnqueueAndPlayPlaylistAsync(media); + } + else + { + ClearPlaylist(); + await EnqueueAndPlay(_delayPlay); + } } _dispatcherQueue.TryEnqueue(SetPlayQueue); @@ -187,7 +195,15 @@ public async void Receive(PlayFilesMessage message) // If there is only 1 file, play it immediately // Avoid waiting to get all the neighboring files then play, which may cause delay ClearPlaylist(); - await EnqueueAndPlay(file); + if (_mediaPlayer == null) + { + _delayPlay = media; + } + else + { + await EnqueueAndPlay(file); + } + // If there are more than one item in the queue, file is already a playlist, no need to check for neighboring files if (Items.Count > 1) return; @@ -642,6 +658,12 @@ private async Task EnqueueAndPlay(object value) } // If playNext is a playlist file, recursively parse the playlist and enqueue the items + await TryEnqueueAndPlayPlaylistAsync(playNext ?? value); + } + + private async Task TryEnqueueAndPlayPlaylistAsync(object value) + { + MediaViewModel? playNext = GetMedia(value); PlaylistCreateResult? result = (value as PlaylistCreateResult) ?? await CreatePlaylistAsync(playNext ?? value); if (result != null && !result.PlayNext.Source.Equals(playNext?.Source)) {