Migrating the Player to be queue-based #39
This commit is contained in:
@@ -23,19 +23,26 @@ namespace Mear.Playback
|
||||
public class MearPlayer
|
||||
{
|
||||
#region Fields
|
||||
private Queue<Song> _mearQueue = null;
|
||||
private static Queue<Song> _mearQueue = null;
|
||||
private static Song _song;
|
||||
private static bool? _initialized = null;
|
||||
private static SortedDictionary<MusicViews, bool?> _songChanged =
|
||||
new SortedDictionary<MusicViews, bool?>{
|
||||
{ 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<Song> songs)
|
||||
{
|
||||
if (_mearQueue == null)
|
||||
{
|
||||
_mearQueue = new Queue<Song>();
|
||||
}
|
||||
_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
|
||||
}
|
||||
|
||||
@@ -40,6 +40,11 @@ namespace Mear.ViewModels
|
||||
|
||||
|
||||
#region Methods
|
||||
public async void UpdateSongAttributes(Song song)
|
||||
{
|
||||
_song.Clear();
|
||||
_song.Add(song);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user