Migrating the Player to be queue-based #39
This commit is contained in:
@@ -23,19 +23,26 @@ namespace Mear.Playback
|
|||||||
public class MearPlayer
|
public class MearPlayer
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private Queue<Song> _mearQueue = null;
|
private static Queue<Song> _mearQueue = null;
|
||||||
private static Song _song;
|
private static Song _song;
|
||||||
private static bool? _initialized = null;
|
private static bool? _initialized = null;
|
||||||
private static SortedDictionary<MusicViews, bool?> _songChanged =
|
private static SortedDictionary<MusicViews, bool?> _songChanged =
|
||||||
new SortedDictionary<MusicViews, bool?>{
|
new SortedDictionary<MusicViews, bool?>{
|
||||||
{ MusicViews.Song, false },
|
{ MusicViews.Song, false },
|
||||||
{ MusicViews.Album, false },
|
{ MusicViews.Album, false },
|
||||||
{ MusicViews.Artist, false }
|
{ MusicViews.Artist, false },
|
||||||
|
{ MusicViews.Player, false }
|
||||||
};
|
};
|
||||||
|
private static int _songIndex;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
public static Song OnSong
|
||||||
|
{
|
||||||
|
get => _song;
|
||||||
|
set => _song = value;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -113,7 +120,17 @@ namespace Mear.Playback
|
|||||||
// TODO: Implement shuffling
|
// TODO: Implement shuffling
|
||||||
break;
|
break;
|
||||||
case PlayControls.NEXT:
|
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;
|
break;
|
||||||
case PlayControls.PREVIOUS:
|
case PlayControls.PREVIOUS:
|
||||||
break;
|
break;
|
||||||
@@ -159,6 +176,16 @@ namespace Mear.Playback
|
|||||||
return null;
|
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()
|
public static async Task DownloadSongToFS()
|
||||||
{
|
{
|
||||||
var songRepo = new RemoteSongRepository();
|
var songRepo = new RemoteSongRepository();
|
||||||
@@ -368,12 +395,17 @@ namespace Mear.Playback
|
|||||||
case Repeat.ONE:
|
case Repeat.ONE:
|
||||||
CrossMediaManager.Current.SeekToStart();
|
CrossMediaManager.Current.SeekToStart();
|
||||||
PlaySong(_song.SongPath);
|
PlaySong(_song.SongPath);
|
||||||
|
return;
|
||||||
break;
|
break;
|
||||||
case Repeat.ALL:
|
case Repeat.ALL:
|
||||||
// Will implment this fully later once Queues are a feature
|
// Will implment this fully later once Queues are a feature
|
||||||
PlaySong(_song.SongPath);
|
PlaySong(_song.SongPath);
|
||||||
|
return;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var song = _mearQueue.ToArray()[_songIndex++];
|
||||||
|
PlaySong(song.SongPath);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
#endregion
|
#endregion
|
||||||
@@ -384,7 +416,8 @@ namespace Mear.Playback
|
|||||||
{
|
{
|
||||||
Song = 0,
|
Song = 0,
|
||||||
Album,
|
Album,
|
||||||
Artist
|
Artist,
|
||||||
|
Player
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ namespace Mear.ViewModels
|
|||||||
|
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
public async void UpdateSongAttributes(Song song)
|
||||||
|
{
|
||||||
|
_song.Clear();
|
||||||
|
_song.Add(song);
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
|||||||
using Xamarin.Forms;
|
using Xamarin.Forms;
|
||||||
|
|
||||||
using Mear.Models;
|
using Mear.Models;
|
||||||
|
using Mear.Playback;
|
||||||
using Mear.Repositories.Local;
|
using Mear.Repositories.Local;
|
||||||
using Mear.Repositories.Remote;
|
using Mear.Repositories.Remote;
|
||||||
|
|
||||||
@@ -131,6 +132,8 @@ namespace Mear.ViewModels
|
|||||||
_songs = rmtSongs;
|
_songs = rmtSongs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MearPlayer.LoadQueue(_songs);
|
||||||
|
|
||||||
_songItems.Clear();
|
_songItems.Clear();
|
||||||
|
|
||||||
_songs.ToList().ForEach(_songItems.Add);
|
_songs.ToList().ForEach(_songItems.Add);
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ namespace Mear.Views
|
|||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private MearPlayerViewModel _viewModel;
|
private MearPlayerViewModel _viewModel;
|
||||||
private Song _song;
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -46,10 +45,11 @@ namespace Mear.Views
|
|||||||
public MearPlayerView(Song song)
|
public MearPlayerView(Song song)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_song = song;
|
MearPlayer.OnSong = song;
|
||||||
InitializeControls();
|
InitializeControls();
|
||||||
|
|
||||||
BackgroundSongElasping();
|
BackgroundSongElasping();
|
||||||
|
BackgroundSongAttributes();
|
||||||
//BackgroundSongCoverUpdate();
|
//BackgroundSongCoverUpdate();
|
||||||
//BackgroundControlInit();
|
//BackgroundControlInit();
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ namespace Mear.Views
|
|||||||
}
|
}
|
||||||
private async void InitializeOptions()
|
private async void InitializeOptions()
|
||||||
{
|
{
|
||||||
if (!_song.Downloaded)
|
if (!MearPlayer.OnSong.Downloaded)
|
||||||
{
|
{
|
||||||
var dnloadOpt = DownloadOption();
|
var dnloadOpt = DownloadOption();
|
||||||
ToolbarItems.Add(dnloadOpt);
|
ToolbarItems.Add(dnloadOpt);
|
||||||
@@ -99,7 +99,7 @@ namespace Mear.Views
|
|||||||
Shuffle.Text = MearPlayer.RetrieveShuffleString();
|
Shuffle.Text = MearPlayer.RetrieveShuffleString();
|
||||||
Repeat.Text = MearPlayer.RetrieveRepeatString();
|
Repeat.Text = MearPlayer.RetrieveRepeatString();
|
||||||
|
|
||||||
EndTime.Text = TimeFormat.ConvertToSongTime(_song.Duration.Value);
|
EndTime.Text = TimeFormat.ConvertToSongTime(MearPlayer.OnSong.Duration.Value);
|
||||||
}
|
}
|
||||||
private void RemoveSyncToolbar()
|
private void RemoveSyncToolbar()
|
||||||
{
|
{
|
||||||
@@ -133,6 +133,41 @@ namespace Mear.Views
|
|||||||
var msg = ex.Message;
|
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()
|
private async Task BackgroundSongCoverUpdate()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -147,7 +182,7 @@ namespace Mear.Views
|
|||||||
if (SongCover.Source.IsEmpty)
|
if (SongCover.Source.IsEmpty)
|
||||||
{
|
{
|
||||||
var meta = new SongMetadataRetriever();
|
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())
|
if (CrossMediaManager.Current.IsPlaying())
|
||||||
{
|
{
|
||||||
await MearPlayer.ControlMusic(_song, PlayControls.PAUSE);
|
await MearPlayer.ControlMusic(MearPlayer.OnSong, PlayControls.PAUSE);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await MearPlayer.ControlMusic(_song, PlayControls.RESUME);
|
await MearPlayer.ControlMusic(MearPlayer.OnSong, PlayControls.RESUME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void Next_Clicked(object sender, EventArgs e)
|
private void Next_Clicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
MearPlayer.ControlMusic(null, PlayControls.NEXT);
|
||||||
}
|
}
|
||||||
private void Repeat_Clicked(object sender, EventArgs e)
|
private void Repeat_Clicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
MearPlayer.ControlMusic(_song, PlayControls.REPEAT);
|
MearPlayer.ControlMusic(MearPlayer.OnSong, PlayControls.REPEAT);
|
||||||
Repeat.Text = MearPlayer.RetrieveRepeatString();
|
Repeat.Text = MearPlayer.RetrieveRepeatString();
|
||||||
}
|
}
|
||||||
private void Shuffle_Clicked(object sender, EventArgs e)
|
private void Shuffle_Clicked(object sender, EventArgs e)
|
||||||
@@ -228,21 +263,21 @@ namespace Mear.Views
|
|||||||
RemoveSyncToolbar();
|
RemoveSyncToolbar();
|
||||||
ToolbarItems.Add(RemoveOption());
|
ToolbarItems.Add(RemoveOption());
|
||||||
|
|
||||||
_song.Downloaded = true;
|
MearPlayer.OnSong.Downloaded = true;
|
||||||
}
|
}
|
||||||
private async void Remove_Clicked(object sender, EventArgs e)
|
private async void Remove_Clicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
RemoveSyncToolbar();
|
RemoveSyncToolbar();
|
||||||
await MearPlayer.RemoveSongFromFS();
|
await MearPlayer.RemoveSongFromFS();
|
||||||
|
|
||||||
_song.Downloaded = false;
|
MearPlayer.OnSong.Downloaded = false;
|
||||||
|
|
||||||
ToolbarItems.Add(DownloadOption());
|
ToolbarItems.Add(DownloadOption());
|
||||||
}
|
}
|
||||||
private async void PlayCount_Clicked(object sender, EventArgs e)
|
private async void PlayCount_Clicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
var playCountRepo = new DBPlayCountRepository();
|
var playCountRepo = new DBPlayCountRepository();
|
||||||
var plyCount = playCountRepo.RetrievePlayCount(_song.Id);
|
var plyCount = playCountRepo.RetrievePlayCount(MearPlayer.OnSong.Id);
|
||||||
|
|
||||||
if (plyCount == null)
|
if (plyCount == null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user