From 3923f81ed41bdbd33a4a533062f4e57d0c7d8d37 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Thu, 20 Jun 2019 17:03:24 -0700 Subject: [PATCH] Queue system is functioning #39 --- Mear/Mear.Android/MainActivity.cs | 1 - Mear/Mear/Playback/MearPlayer.cs | 125 +++++++++++-------------- Mear/Mear/Views/MearPlayerView.xaml.cs | 6 +- Mear/Mear/Views/SongView.xaml.cs | 2 + 4 files changed, 58 insertions(+), 76 deletions(-) diff --git a/Mear/Mear.Android/MainActivity.cs b/Mear/Mear.Android/MainActivity.cs index 04d0575..eddcb3f 100644 --- a/Mear/Mear.Android/MainActivity.cs +++ b/Mear/Mear.Android/MainActivity.cs @@ -29,7 +29,6 @@ namespace Mear.Droid global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); - CrossMediaManager.Current.Init(); CrossMediaManager.Current.Init(this); } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) diff --git a/Mear/Mear/Playback/MearPlayer.cs b/Mear/Mear/Playback/MearPlayer.cs index 44e8a62..fae9c4d 100644 --- a/Mear/Mear/Playback/MearPlayer.cs +++ b/Mear/Mear/Playback/MearPlayer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -43,6 +44,30 @@ namespace Mear.Playback get => _song; set => _song = value; } + + private static int QueueIndex + { + get => _songIndex; + set + { + if (value >= QueueCount) + { + _songIndex = 0; + } + else if (value < 0) + { + _songIndex = QueueCount; + } + else + { + _songIndex = value; + } + } + } + private static int QueueCount + { + get => _mearQueue.Count; + } #endregion @@ -51,44 +76,6 @@ namespace Mear.Playback #region Methods - public static async Task StreamSongDemoAsync(Song song) - { - string tmpFile = Path.GetTempPath() + "track.mp3"; - - if (File.Exists(tmpFile)) - { - File.Delete(tmpFile); - } - - try - { - using (var writer = File.OpenWrite(tmpFile)) - { - var client = new RestClient(API.ApiUrl); - var apiEndpoint = $@"api/{API.APIVersion}/song/stream/{song.Id}"; ; - var request = new RestRequest(apiEndpoint, Method.GET); - var tokRepo = new DBTokenRepository(); - var token = tokRepo.RetrieveToken(); - request.AddHeader("Authorization", $"Bearer {token.AccessToken}"); - request.ResponseWriter = (responseStream) => - responseStream.CopyTo(writer); - - var response = client.DownloadData(request); - - await CrossMediaManager.Current.Play(tmpFile); - song.SongPath = tmpFile; - - } - - return song; - } - catch (Exception ex) - { - var msg = ex.Message; - } - - return null; - } public static async Task ControlMusic(Song song, PlayControls control) { if (song != null) @@ -98,7 +85,7 @@ namespace Mear.Playback switch (control) { case PlayControls.PLAYOFFLINE: - await PlaySong(song); + DeterminePlayType(); var plyCountRepo = new DBPlayCountRepository(); plyCountRepo.AffectPlayCount(song); InitializeRepeatMode(); @@ -110,9 +97,8 @@ namespace Mear.Playback ResumeSong(); break; case PlayControls.STREAM: - song = StreamSong(song); InitializeRepeatMode(); - return song; + return StreamSong(song); case PlayControls.REPEAT: ToggleRepeat(); break; @@ -120,30 +106,22 @@ namespace Mear.Playback // TODO: Implement shuffling break; case PlayControls.NEXT: - song = _mearQueue.ToArray()[_songIndex++]; - if (!song.Downloaded) - { - song = StreamSong(song); - } - else - { - PlaySong(song.SongPath); - } - _song = song; + _song = _mearQueue.ToArray()[++QueueIndex]; + DeterminePlayType(); + _songChanged[MusicViews.Player] = true; break; case PlayControls.PREVIOUS: + _song = _mearQueue.ToArray()[--QueueIndex]; + DeterminePlayType(); + + _songChanged[MusicViews.Player] = true; break; - // TODO: Implement Next } return null; } - public static async Task SongTitle() - { - return _song.Title; - } public static async Task ConvertToTime() { try @@ -176,6 +154,11 @@ namespace Mear.Playback return null; } + public static async Task AlterIndex(Song song) + { + QueueIndex = _mearQueue.ToArray().ToList().IndexOf(song); + } + public static async Task LoadQueue(List songs) { if (_mearQueue == null) @@ -193,19 +176,6 @@ namespace Mear.Playback _song.Downloaded = true; } - public static async Task PlaySong(Song song) - { - try - { - var songPath = song.SongPath; - - PlaySong(songPath); - } - catch (Exception ex) - { - var msg = ex.Message; - } - } public static async Task RemoveSongFromFS() { var dbSongRepo = new DBSongRepository(); @@ -324,6 +294,17 @@ namespace Mear.Playback return null; } + private static void DeterminePlayType() + { + if (!_song.Downloaded) + { + StreamSong(_song); + } + else + { + PlaySong(_song.SongPath); + } + } private static void InitializeModes() { InitializeRepeatMode(); @@ -396,15 +377,13 @@ namespace Mear.Playback 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++]; + var song = _mearQueue.ToArray()[++QueueIndex]; if (song.Downloaded) { PlaySong(song.SongPath); @@ -413,6 +392,8 @@ namespace Mear.Playback { StreamSong(song); } + + _songChanged[MusicViews.Player] = true; } #endregion #endregion diff --git a/Mear/Mear/Views/MearPlayerView.xaml.cs b/Mear/Mear/Views/MearPlayerView.xaml.cs index bf7d9c2..29ba413 100644 --- a/Mear/Mear/Views/MearPlayerView.xaml.cs +++ b/Mear/Mear/Views/MearPlayerView.xaml.cs @@ -47,9 +47,9 @@ namespace Mear.Views InitializeComponent(); MearPlayer.OnSong = song; - Initialize(); - BindingContext = _viewModel = new MearPlayerViewModel(song); + + Initialize(); } #endregion @@ -226,7 +226,7 @@ namespace Mear.Views #region Events private void Previous_Clicked(object sender, EventArgs e) { - + MearPlayer.ControlMusic(null, PlayControls.PREVIOUS); } private async void Play_Clicked(object sender, EventArgs e) { diff --git a/Mear/Mear/Views/SongView.xaml.cs b/Mear/Mear/Views/SongView.xaml.cs index d0eb4d6..93aece0 100644 --- a/Mear/Mear/Views/SongView.xaml.cs +++ b/Mear/Mear/Views/SongView.xaml.cs @@ -159,6 +159,8 @@ namespace Mear.Views song = await MearPlayer.ControlMusic(song, PlayControls.STREAM); } + MearPlayer.AlterIndex(song); + Navigation.PushModalAsync(new NavigationPage(new MearPlayerView(song))); } catch (Exception ex)