From 17f07b5040fdf0296fd380354696ae3d30e601fc Mon Sep 17 00:00:00 2001 From: amazing-username Date: Mon, 17 Jun 2019 17:08:24 -0700 Subject: [PATCH] Migrating the Player to be queue-based #39 --- Mear/Mear/Playback/MearPlayer.cs | 41 ++++++++++++-- Mear/Mear/ViewModels/MearPlayerViewModel.cs | 5 ++ Mear/Mear/ViewModels/SongViewModel.cs | 3 ++ Mear/Mear/Views/MearPlayerView.xaml.cs | 59 ++++++++++++++++----- 4 files changed, 92 insertions(+), 16 deletions(-) diff --git a/Mear/Mear/Playback/MearPlayer.cs b/Mear/Mear/Playback/MearPlayer.cs index ad01a8b..aec54ea 100644 --- a/Mear/Mear/Playback/MearPlayer.cs +++ b/Mear/Mear/Playback/MearPlayer.cs @@ -23,19 +23,26 @@ namespace Mear.Playback public class MearPlayer { #region Fields - private Queue _mearQueue = null; + private static Queue _mearQueue = null; private static Song _song; private static bool? _initialized = null; private static SortedDictionary _songChanged = new SortedDictionary{ { MusicViews.Song, false }, { MusicViews.Album, false }, - { MusicViews.Artist, false } + { MusicViews.Artist, false }, + { MusicViews.Player, false } }; + private static int _songIndex; #endregion #region Properties + public static Song OnSong + { + get => _song; + set => _song = value; + } #endregion @@ -113,7 +120,17 @@ namespace Mear.Playback // TODO: Implement shuffling break; case PlayControls.NEXT: - // TODO: Implement Next + song = _mearQueue.ToArray()[_songIndex++]; + if (!song.Downloaded) + { + song = StreamSong(song); + } + else + { + PlaySong(song.SongPath); + } + _song = song; + _songChanged[MusicViews.Player] = true; break; case PlayControls.PREVIOUS: break; @@ -159,6 +176,16 @@ namespace Mear.Playback return null; } + public static async Task LoadQueue(List songs) + { + if (_mearQueue == null) + { + _mearQueue = new Queue(); + } + _mearQueue.Clear(); + songs.ForEach(_mearQueue.Enqueue); + _songIndex = 0; + } public static async Task DownloadSongToFS() { var songRepo = new RemoteSongRepository(); @@ -368,12 +395,17 @@ namespace Mear.Playback case Repeat.ONE: CrossMediaManager.Current.SeekToStart(); PlaySong(_song.SongPath); + return; break; case Repeat.ALL: // Will implment this fully later once Queues are a feature PlaySong(_song.SongPath); + return; break; } + + var song = _mearQueue.ToArray()[_songIndex++]; + PlaySong(song.SongPath); } #endregion #endregion @@ -384,7 +416,8 @@ namespace Mear.Playback { Song = 0, Album, - Artist + Artist, + Player } #endregion } diff --git a/Mear/Mear/ViewModels/MearPlayerViewModel.cs b/Mear/Mear/ViewModels/MearPlayerViewModel.cs index 4165602..33ed8ff 100644 --- a/Mear/Mear/ViewModels/MearPlayerViewModel.cs +++ b/Mear/Mear/ViewModels/MearPlayerViewModel.cs @@ -40,6 +40,11 @@ namespace Mear.ViewModels #region Methods + public async void UpdateSongAttributes(Song song) + { + _song.Clear(); + _song.Add(song); + } #endregion } } diff --git a/Mear/Mear/ViewModels/SongViewModel.cs b/Mear/Mear/ViewModels/SongViewModel.cs index 4e8ce56..d2dd93f 100644 --- a/Mear/Mear/ViewModels/SongViewModel.cs +++ b/Mear/Mear/ViewModels/SongViewModel.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Xamarin.Forms; using Mear.Models; +using Mear.Playback; using Mear.Repositories.Local; using Mear.Repositories.Remote; @@ -131,6 +132,8 @@ namespace Mear.ViewModels _songs = rmtSongs; } + MearPlayer.LoadQueue(_songs); + _songItems.Clear(); _songs.ToList().ForEach(_songItems.Add); diff --git a/Mear/Mear/Views/MearPlayerView.xaml.cs b/Mear/Mear/Views/MearPlayerView.xaml.cs index 32401cf..5838867 100644 --- a/Mear/Mear/Views/MearPlayerView.xaml.cs +++ b/Mear/Mear/Views/MearPlayerView.xaml.cs @@ -28,7 +28,6 @@ namespace Mear.Views { #region Fields private MearPlayerViewModel _viewModel; - private Song _song; #endregion @@ -46,10 +45,11 @@ namespace Mear.Views public MearPlayerView(Song song) { InitializeComponent(); - _song = song; + MearPlayer.OnSong = song; InitializeControls(); BackgroundSongElasping(); + BackgroundSongAttributes(); //BackgroundSongCoverUpdate(); //BackgroundControlInit(); @@ -83,7 +83,7 @@ namespace Mear.Views } private async void InitializeOptions() { - if (!_song.Downloaded) + if (!MearPlayer.OnSong.Downloaded) { var dnloadOpt = DownloadOption(); ToolbarItems.Add(dnloadOpt); @@ -99,7 +99,7 @@ namespace Mear.Views Shuffle.Text = MearPlayer.RetrieveShuffleString(); Repeat.Text = MearPlayer.RetrieveRepeatString(); - EndTime.Text = TimeFormat.ConvertToSongTime(_song.Duration.Value); + EndTime.Text = TimeFormat.ConvertToSongTime(MearPlayer.OnSong.Duration.Value); } private void RemoveSyncToolbar() { @@ -133,6 +133,41 @@ namespace Mear.Views var msg = ex.Message; } } + private async Task BackgroundSongAttributes() + { + try + { + new Thread(async () => + { + while (true) + { + Device.BeginInvokeOnMainThread(async () => + { + if (MearPlayer.SongHasBeenChanged(MearPlayer.MusicViews.Player)) + { + RemoveSyncToolbar(); + if (MearPlayer.OnSong.Downloaded) + { + ToolbarItems.Add(RemoveOption()); + } + { + ToolbarItems.Add(DownloadOption()); + } + _viewModel.UpdateSongAttributes(MearPlayer.OnSong); + MearPlayer.ResetSongChange(MearPlayer.MusicViews.Player); + } + }); + await Task.Delay(500); + } + + }).Start(); + + } + catch (Exception ex) + { + var msg = ex.Message; + } + } private async Task BackgroundSongCoverUpdate() { try @@ -147,7 +182,7 @@ namespace Mear.Views if (SongCover.Source.IsEmpty) { var meta = new SongMetadataRetriever(); - var data = meta.ExtractCoverArtData(_song); + var data = meta.ExtractCoverArtData(MearPlayer.OnSong); } }); @@ -191,20 +226,20 @@ namespace Mear.Views { if (CrossMediaManager.Current.IsPlaying()) { - await MearPlayer.ControlMusic(_song, PlayControls.PAUSE); + await MearPlayer.ControlMusic(MearPlayer.OnSong, PlayControls.PAUSE); } else { - await MearPlayer.ControlMusic(_song, PlayControls.RESUME); + await MearPlayer.ControlMusic(MearPlayer.OnSong, PlayControls.RESUME); } } private void Next_Clicked(object sender, EventArgs e) { - + MearPlayer.ControlMusic(null, PlayControls.NEXT); } private void Repeat_Clicked(object sender, EventArgs e) { - MearPlayer.ControlMusic(_song, PlayControls.REPEAT); + MearPlayer.ControlMusic(MearPlayer.OnSong, PlayControls.REPEAT); Repeat.Text = MearPlayer.RetrieveRepeatString(); } private void Shuffle_Clicked(object sender, EventArgs e) @@ -228,21 +263,21 @@ namespace Mear.Views RemoveSyncToolbar(); ToolbarItems.Add(RemoveOption()); - _song.Downloaded = true; + MearPlayer.OnSong.Downloaded = true; } private async void Remove_Clicked(object sender, EventArgs e) { RemoveSyncToolbar(); await MearPlayer.RemoveSongFromFS(); - _song.Downloaded = false; + MearPlayer.OnSong.Downloaded = false; ToolbarItems.Add(DownloadOption()); } private async void PlayCount_Clicked(object sender, EventArgs e) { var playCountRepo = new DBPlayCountRepository(); - var plyCount = playCountRepo.RetrievePlayCount(_song.Id); + var plyCount = playCountRepo.RetrievePlayCount(MearPlayer.OnSong.Id); if (plyCount == null) {