Added playing song info view and view model.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Media.Imaging;
|
||||
using Avalonia.Media.Imaging;
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Harmonia.Core.Caching;
|
||||
@@ -145,27 +144,6 @@ public partial class PlaybackBarViewModel : ViewModelBase
|
||||
_audioImageCache = audioImageCache;
|
||||
|
||||
_timer = new(TimeSpan.FromMilliseconds(100), DispatcherPriority.Default, TickTock);
|
||||
|
||||
PlayDemoSong(playlistRepository, audioFileScanner);
|
||||
}
|
||||
|
||||
private async Task PlayDemoSong(IPlaylistRepository playlistRepository, IAudioFileScanner audioFileScanner, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (playlistRepository.Get().Count == 0)
|
||||
{
|
||||
playlistRepository.AddPlaylist();
|
||||
|
||||
Playlist playlist = playlistRepository.Get().First();
|
||||
|
||||
//string songPath = @"D:\Music\Game Music\Bobby Prince\Doom II";
|
||||
//string songPath = @"D:\Music\Anime Music\HimeHina";
|
||||
string songPath = @"D:\Music\K-Pop";
|
||||
|
||||
Song[] songs = await audioFileScanner.GetSongsFromPathAsync(songPath, cancellationToken);
|
||||
playlist.AddSongs(songs);
|
||||
}
|
||||
|
||||
await _audioPlayer.LoadAsync(playlistRepository.Get().First().Songs[0], PlaybackMode.LoadAndPlay);
|
||||
}
|
||||
|
||||
private void OnAudioPlayerPlayingSongChanged(object? sender, EventArgs e)
|
||||
|
||||
91
Harmonia.UI/ViewModels/PlayingSongInfoViewModel.cs
Normal file
91
Harmonia.UI/ViewModels/PlayingSongInfoViewModel.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using Avalonia.Media.Imaging;
|
||||
using Avalonia.Threading;
|
||||
using Harmonia.Core.Caching;
|
||||
using Harmonia.Core.Imaging;
|
||||
using Harmonia.Core.Models;
|
||||
using Harmonia.Core.Player;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Harmonia.UI.ViewModels;
|
||||
|
||||
public class PlayingSongInfoViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IAudioPlayer _audioPlayer;
|
||||
private readonly IAudioImageCache _audioImageCache;
|
||||
|
||||
private CancellationTokenSource? _audioImageCancellationTokenSource;
|
||||
|
||||
private Song? _song;
|
||||
public Song? Song
|
||||
{
|
||||
get
|
||||
{
|
||||
return _song;
|
||||
}
|
||||
private set
|
||||
{
|
||||
_song = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private Bitmap? _songImageSource;
|
||||
public Bitmap? SongImageSource
|
||||
{
|
||||
get
|
||||
{
|
||||
return _songImageSource;
|
||||
}
|
||||
private set
|
||||
{
|
||||
_songImageSource = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public PlayingSongInfoViewModel(IAudioPlayer audioPlayer, IAudioImageCache audioImageCache)
|
||||
{
|
||||
_audioPlayer = audioPlayer;
|
||||
_audioPlayer.PlayingSongChanged += OnAudioPlayerPlayingSongChanged;
|
||||
|
||||
_audioImageCache = audioImageCache;
|
||||
}
|
||||
|
||||
private void OnAudioPlayerPlayingSongChanged(object? sender, EventArgs e)
|
||||
{
|
||||
Song = _audioPlayer.PlayingSong?.Song;
|
||||
Task.Run(UpdateImage);
|
||||
}
|
||||
|
||||
private async Task UpdateImage()
|
||||
{
|
||||
// TODO: Show default picture
|
||||
if (Song == null)
|
||||
return;
|
||||
|
||||
if (_audioImageCancellationTokenSource != null)
|
||||
await _audioImageCancellationTokenSource.CancelAsync();
|
||||
|
||||
_audioImageCancellationTokenSource = new();
|
||||
CancellationToken cancellationToken = _audioImageCancellationTokenSource.Token;
|
||||
|
||||
SongPictureInfo? songPictureInfo = await _audioImageCache.GetAsync(Song, cancellationToken);
|
||||
|
||||
if (songPictureInfo == null)
|
||||
return;
|
||||
|
||||
await Dispatcher.UIThread.InvokeAsync(() => SetSongImageSource(songPictureInfo));
|
||||
}
|
||||
|
||||
private void SetSongImageSource(SongPictureInfo songPictureInfo)
|
||||
{
|
||||
if (songPictureInfo.Data.Length == 0)
|
||||
return;
|
||||
|
||||
using MemoryStream stream = new(songPictureInfo.Data);
|
||||
SongImageSource = new(stream);
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,7 @@ public class ViewModelLocator
|
||||
|
||||
public static PlaybackBarViewModel PlaybackBarViewModel
|
||||
=> App.ServiceProvider.GetRequiredService<PlaybackBarViewModel>();
|
||||
|
||||
public static PlayingSongInfoViewModel PlayingSongInfoViewModel
|
||||
=> App.ServiceProvider.GetRequiredService<PlayingSongInfoViewModel>();
|
||||
}
|
||||
Reference in New Issue
Block a user