Various updates.

This commit is contained in:
2025-03-30 23:18:40 -04:00
parent b7ba8341a1
commit f7cacf0bbb
11 changed files with 322 additions and 57 deletions

View File

@@ -12,6 +12,8 @@ using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.System;
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
namespace Harmonia.WinUI.ViewModels;
@@ -20,6 +22,7 @@ public partial class PlayerViewModel : ViewModelBase
private readonly IAudioPlayer _audioPlayer;
private readonly IAudioBitmapImageCache _audioBitmapImageCache;
private readonly DispatcherTimer _timer;
private readonly DispatcherQueue _dispatcherQueue;
private CancellationTokenSource? _songImageCancellationTokenSource;
@@ -185,6 +188,18 @@ public partial class PlayerViewModel : ViewModelBase
}
}
public bool CanUpdatePosition
{
get
{
return _audioPlayer.State != AudioPlaybackState.Stopped;
}
private set
{
OnPropertyChanged();
}
}
public ICommand PlaySongCommand => new RelayCommand(Play);
public ICommand PauseSongCommand => new RelayCommand(Pause);
public ICommand StopSongCommand => new RelayCommand(Stop);
@@ -208,6 +223,8 @@ public partial class PlayerViewModel : ViewModelBase
};
_timer.Tick += TickTock;
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
}
#region Event Handlers
@@ -230,9 +247,13 @@ public partial class PlayerViewModel : ViewModelBase
_songImageCancellationTokenSource = new();
CancellationToken cancellationToken = _songImageCancellationTokenSource.Token;
BitmapImage? bitmapImage = await _audioBitmapImageCache.GetAsync(Song, cancellationToken);
//BitmapImage? bitmapImage = await _audioBitmapImageCache.GetAsync(Song, cancellationToken);
DispatcherQueue.GetForCurrentThread().TryEnqueue(() => SongImageSource = bitmapImage);
_dispatcherQueue.TryEnqueue(async () =>
{
BitmapImage? bitmapImage = await _audioBitmapImageCache.GetAsync(Song, cancellationToken);
SongImageSource = bitmapImage;
});
}
private void OnAudioPlayerPropertyChanged(object? sender, PropertyChangedEventArgs e)
@@ -241,6 +262,7 @@ public partial class PlayerViewModel : ViewModelBase
{
case nameof(_audioPlayer.State):
UpdateTimer();
OnPropertyChanged(nameof(CanUpdatePosition));
break;
}
}