Added tracking/capturing/restoration of audio playback state and position.
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
namespace Harmonia.WinUI.Session;
|
||||
using Harmonia.Core.Engine;
|
||||
|
||||
namespace Harmonia.WinUI.Session;
|
||||
|
||||
public partial class AudioPlayerState
|
||||
{
|
||||
public string? PlaylistUID { get; set; }
|
||||
public string? PlaylistSongUID { get; set; }
|
||||
public double Position { get; set; } = 0.0;
|
||||
public string? PlaybackState { get; set; }
|
||||
public AudioPlaybackState PlaybackState { get; set; } = AudioPlaybackState.Stopped;
|
||||
public double Volume { get; set; } = 1.0;
|
||||
public bool IsMuted { get; set; }
|
||||
public string? RepeatState { get; set; }
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
namespace Harmonia.WinUI.Session;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Harmonia.WinUI.Session;
|
||||
|
||||
public interface ISessionRestorer
|
||||
{
|
||||
void Restore();
|
||||
Task RestoreAsync();
|
||||
}
|
||||
@@ -3,4 +3,5 @@ namespace Harmonia.WinUI.Session;
|
||||
public interface ISessionTracker
|
||||
{
|
||||
void Start();
|
||||
void Capture();
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using Harmonia.Core.Player;
|
||||
using Harmonia.Core.Engine;
|
||||
using Harmonia.Core.Player;
|
||||
using Harmonia.Core.Playlists;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Harmonia.WinUI.Session;
|
||||
|
||||
@@ -10,10 +12,10 @@ public class SessionRestorer(
|
||||
IPlaylistManager playlistManager,
|
||||
IAudioPlayer audioPlayer) : ISessionRestorer
|
||||
{
|
||||
public void Restore()
|
||||
public async Task RestoreAsync()
|
||||
{
|
||||
RestorePlaylist();
|
||||
RestoreAudioPlayer();
|
||||
await RestoreAudioPlayerAsync();
|
||||
}
|
||||
|
||||
private void RestorePlaylist()
|
||||
@@ -31,7 +33,7 @@ public class SessionRestorer(
|
||||
playlistManager.CurrentPlaylist = playlist;
|
||||
}
|
||||
|
||||
private void RestoreAudioPlayer()
|
||||
private async Task RestoreAudioPlayerAsync()
|
||||
{
|
||||
AudioPlayerState audioPlayerState = sessionService.Session.Player;
|
||||
|
||||
@@ -43,13 +45,27 @@ public class SessionRestorer(
|
||||
if (playlist is null)
|
||||
return;
|
||||
|
||||
PlaylistSong? song = playlist.Songs.FirstOrDefault(s => s.UID == audioPlayerState.PlaylistSongUID);
|
||||
PlaylistSong? playlistSong = playlist.Songs.FirstOrDefault(s => s.UID == audioPlayerState.PlaylistSongUID);
|
||||
|
||||
if (song is null)
|
||||
if (playlistSong is null)
|
||||
return;
|
||||
|
||||
//PlaybackMode playbackMode = audioPlayerState.PlaybackState
|
||||
await audioPlayer.LoadAsync(playlistSong, PlaybackMode.LoadOnly);
|
||||
|
||||
audioPlayer.LoadAsync(song, PlaybackMode.LoadOnly);
|
||||
if (audioPlayerState.Position > 0 && audioPlayerState.Position <= playlistSong.Song.Length.TotalSeconds)
|
||||
audioPlayer.Position = audioPlayerState.Position;
|
||||
|
||||
switch (audioPlayerState.PlaybackState)
|
||||
{
|
||||
case AudioPlaybackState.Playing:
|
||||
audioPlayer.Play();
|
||||
break;
|
||||
case AudioPlaybackState.Paused:
|
||||
audioPlayer.Pause();
|
||||
break;
|
||||
case AudioPlaybackState.Stopped:
|
||||
audioPlayer.Stop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ public class SessionTracker(
|
||||
{
|
||||
playlistManager.CurrentPlaylistChanged += OnCurrentPlaylistChanged;
|
||||
audioPlayer.PlayingSongChanged += OnPlayingSongChanged;
|
||||
audioPlayer.PropertyChanged += OnAudioPlayerPropertyChanged;
|
||||
}
|
||||
|
||||
private void OnCurrentPlaylistChanged(object? sender, EventArgs e)
|
||||
@@ -26,4 +27,19 @@ public class SessionTracker(
|
||||
AudioPlayerState audioPlayerState = sessionService.Session.Player;
|
||||
audioPlayerState.PlaylistSongUID = e.NewSong?.UID;
|
||||
}
|
||||
|
||||
private void OnAudioPlayerPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
||||
{
|
||||
switch (e.PropertyName)
|
||||
{
|
||||
case nameof(IAudioPlayer.State):
|
||||
sessionService.Session.Player.PlaybackState = audioPlayer.State;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Capture()
|
||||
{
|
||||
sessionService.Session.Player.Position = audioPlayer.Position;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user