Making cast automatically resume itself.

This commit is contained in:
Tristan Roux
2018-12-20 22:37:31 +01:00
parent 97014a6491
commit b7a33fcd7e
6 changed files with 128 additions and 86 deletions
+39 -16
View File
@@ -984,12 +984,16 @@ namespace MusicApp
public async void ShowSmallPlayer()
{
Console.WriteLine("&Small Player show is waiting for view");
while (FindViewById(Resource.Id.playerView) == null)
await Task.Delay(1000);
Console.WriteLine("&Showing small player");
Console.WriteLine("&Player deteached: " + Player.instance.IsDetached);
FindViewById<NestedScrollView>(Resource.Id.playerSheet).Visibility = ViewStates.Visible;
FindViewById(Resource.Id.playerView).Alpha = 0;
Player.instance?.RefreshPlayer();
Player.instance.RefreshPlayer();
FindViewById<FrameLayout>(Resource.Id.contentView).SetPadding(0, 0, 0, DpToPx(70));
}
@@ -1478,9 +1482,11 @@ namespace MusicApp
instance = this;
StateSaved = false;
Console.WriteLine("&Resuming Main Activity");
if (CastContext.SessionManager.CurrentSession == null && MusicPlayer.CurrentID() == -1)
MusicPlayer.currentID = MusicPlayer.RetrieveQueueSlot();
else if(CastContext.SessionManager.CurrentSession != null)
else if(MusicPlayer.UseCastPlayer)
MusicPlayer.GetQueueFromCast();
if (SearchableActivity.instance != null && SearchableActivity.instance.searched)
@@ -1564,7 +1570,6 @@ namespace MusicApp
{
Console.WriteLine("&Session Resumed");
SwitchRemote(((CastSession)session).RemoteMediaClient);
MusicPlayer.GetQueueFromCast();
}
public void OnSessionResuming(Java.Lang.Object session, string sessionId) { }
@@ -1585,28 +1590,46 @@ namespace MusicApp
SwitchRemote(null);
}
private void SwitchRemote(RemoteMediaClient remoteClient)
private async void SwitchRemote(RemoteMediaClient remoteClient)
{
Console.WriteLine("&Switching to another remote player: (null check)" + (remoteClient == null));
if (MusicPlayer.instance != null && MusicPlayer.RemotePlayer != null)
MusicPlayer.RemotePlayer.UnregisterCallback(MusicPlayer.CastCallback);
MusicPlayer.RemotePlayer = remoteClient;
MusicPlayer.CastCallback = new CastCallback();
if (MusicPlayer.instance != null && MusicPlayer.RemotePlayer != null)
MusicPlayer.RemotePlayer.RegisterCallback(MusicPlayer.CastCallback);
if (remoteClient != null)
{
if (MusicPlayer.CastCallback == null)
{
MusicPlayer.CastCallback = new CastCallback();
MusicPlayer.RemotePlayer.RegisterCallback(MusicPlayer.CastCallback);
}
if (MusicPlayer.CastQueueManager == null)
{
MusicPlayer.CastQueueManager = new CastQueueManager();
MusicPlayer.RemotePlayer.MediaQueue.RegisterCallback(MusicPlayer.CastQueueManager);
}
}
else
{
if (MusicPlayer.CastCallback != null)
{
MusicPlayer.RemotePlayer.UnregisterCallback(MusicPlayer.CastCallback);
MusicPlayer.CastCallback = null;
}
if (MusicPlayer.CastQueueManager != null)
{
MusicPlayer.RemotePlayer.MediaQueue.UnregisterCallback(MusicPlayer.CastQueueManager);
MusicPlayer.CastQueueManager = null;
}
}
MusicPlayer.UseCastPlayer = MusicPlayer.RemotePlayer != null;
await Task.Delay(1000);
if (MusicPlayer.UseCastPlayer)
{
if (Queue.instance != null)
{
Queue.instance.Refresh();
MusicPlayer.RemotePlayer.MediaQueue.RegisterCallback(new QueueCallback(Queue.instance.adapter));
}
if (Home.instance != null && Home.adapterItems.Count > 0)
Home.adapterItems[0].recycler?.SetAdapter(new LineAdapter(Home.adapterItems[0].recycler));
}
MusicPlayer.GetQueueFromCast();
}
}
}
@@ -9,28 +9,20 @@ namespace MusicApp.Resources.Portable_Class
public override void OnMetadataUpdated()
{
base.OnMetadataUpdated();
//Console.WriteLine("&MetaData Updated");
//if (MusicPlayer.RemotePlayer.CurrentItem != null)
// MusicPlayer.currentID = MusicPlayer.RemotePlayer.MediaQueue.IndexOfItemWithId(MusicPlayer.RemotePlayer.CurrentItem.ItemId);
//else
// MusicPlayer.currentID = -1;
Console.WriteLine("&MetaData Updated");
if (MusicPlayer.RemotePlayer.CurrentItem != null)
MusicPlayer.currentID = MusicPlayer.RemotePlayer.MediaQueue.IndexOfItemWithId(MusicPlayer.RemotePlayer.CurrentItem.ItemId);
//Console.WriteLine("&CurrentID: " + MusicPlayer.currentID);
Console.WriteLine("&CurrentID: " + MusicPlayer.currentID);
Player.instance?.RefreshPlayer();
}
public override void OnPreloadStatusUpdated()
{
base.OnPreloadStatusUpdated();
Console.WriteLine("&Preload Status Updated");
}
public override void OnQueueStatusUpdated()
{
base.OnQueueStatusUpdated();
Console.WriteLine("&Queue status updated");
//MusicPlayer.GetQueueFromCast();
MusicPlayer.GetQueueFromCast();
}
public override void OnSendingRemoteMediaRequest()
@@ -8,6 +8,7 @@ namespace MusicApp.Resources.Portable_Class
public override void ItemsUpdatedAtIndexes(int[] indexes)
{
base.ItemsUpdatedAtIndexes(indexes);
System.Console.WriteLine("&Index has come");
foreach (int index in indexes)
{
if (MusicPlayer.queue.Count > index)
+6 -2
View File
@@ -80,7 +80,7 @@ namespace MusicApp.Resources.Portable_Class
Android.Net.Uri musicUri = MediaStore.Audio.Media.ExternalContentUri;
List<Song> allSongs = new List<Song>();
CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null);
CursorLoader cursorLoader = new CursorLoader(MainActivity.instance, musicUri, null, null, null, null);
ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground();
if (musicCursor != null && musicCursor.MoveToFirst())
@@ -446,7 +446,11 @@ namespace MusicApp.Resources.Portable_Class
public void RefreshQueue()
{
if (adapterItems.Count > 0)
if (MusicPlayer.UseCastPlayer && adapterItems.Count > 0)
{
adapterItems[0].recycler?.SetAdapter(new LineAdapter(adapterItems[0].recycler));
}
else if (adapterItems.Count > 0)
{
adapterItems[0].recycler?.GetAdapter()?.NotifyDataSetChanged();
if (MusicPlayer.CurrentID() != -1 && MusicPlayer.CurrentID() <= MusicPlayer.queue.Count)
@@ -48,6 +48,7 @@ namespace MusicApp.Resources.Portable_Class
public static SimpleExoPlayer player;
public static RemoteMediaClient RemotePlayer;
public static CastCallback CastCallback;
public static CastQueueManager CastQueueManager;
public float volume;
private static AudioStopper noisyReceiver;
public static List<Song> queue = new List<Song>();
@@ -162,6 +163,8 @@ namespace MusicApp.Resources.Portable_Class
break;
case "CastListener":
if (CastCallback == null)
InitializeService();
CastCallback.OnStatusUpdated();
return StartCommandResult.Sticky;
}
@@ -219,9 +222,16 @@ namespace MusicApp.Resources.Portable_Class
RemotePlayer = MainActivity.CastContext.SessionManager.CurrentCastSession?.RemoteMediaClient;
if(RemotePlayer != null)
{
CastCallback = new CastCallback();
RemotePlayer.RegisterCallback(CastCallback);
RemotePlayer.MediaQueue.RegisterCallback(new CastQueueManager());
if (CastCallback == null)
{
CastCallback = new CastCallback();
RemotePlayer.RegisterCallback(CastCallback);
}
if(CastQueueManager == null)
{
CastQueueManager = new CastQueueManager();
RemotePlayer.MediaQueue.RegisterCallback(CastQueueManager);
}
}
UseCastPlayer = RemotePlayer != null;
player.PlayWhenReady = !UseCastPlayer;
@@ -233,7 +243,7 @@ namespace MusicApp.Resources.Portable_Class
player.Volume = volume * (volumeDuked ? 0.2f : 1);
}
public void Play(string filePath, string title = null, string artist = null, string youtubeID = null, string thumbnailURI = null, bool addToQueue = true, bool isLive = false)
public void Play(string filePath, string title = null, string artist = null, string youtubeID = null, string thumbnailURI = null, bool addToQueue = true, bool isLive = false, DateTimeOffset? expireDate = null)
{
isRunning = true;
if (player == null)
@@ -334,8 +344,8 @@ namespace MusicApp.Resources.Portable_Class
{
RemotePlayer.Load(GetMediaInfo(song), new MediaLoadOptions.Builder().SetAutoplay(true).Build());
RemotePlayer.Play();
queue.Insert(song.QueueSlot, song);
currentID = song.QueueSlot;
//queue.Insert(song.QueueSlot, song);
//currentID = song.QueueSlot;
Console.WriteLine("&Song inserted in the queue");
}
@@ -490,8 +500,8 @@ namespace MusicApp.Resources.Portable_Class
var mediaStreamInfo = await client.GetVideoMediaStreamInfosAsync(videoID);
AudioStreamInfo streamInfo = mediaStreamInfo.Audio.OrderBy(s => s.Bitrate).Last();
bool isLive = false;
string streamURL = streamInfo.Url;
if(mediaStreamInfo.HlsLiveStreamUrl != null)
string streamURL = streamInfo.Url;
if (mediaStreamInfo.HlsLiveStreamUrl != null)
{
streamURL = mediaStreamInfo.HlsLiveStreamUrl;
isLive = true;
@@ -506,10 +516,28 @@ namespace MusicApp.Resources.Portable_Class
thumbnailURL = video.Thumbnails.HighResUrl;
}
Console.WriteLine("&Use Cast Player: " + UseCastPlayer);
DateTimeOffset? expireDate = null;
if (UseCastPlayer)
{
Video info = await client.GetVideoAsync(videoID);
thumbnailURL = info.Thumbnails.HighResUrl;
if (artist == null || artist == "")
artist = info.Author;
if (!isLive)
{
expireDate = mediaStreamInfo.ValidUntil;
}
}
Console.WriteLine("&Starting playback");
switch (action)
{
case "Play":
Play(streamURL, title, artist, videoID, thumbnailURL, addToQueue, isLive);
Play(streamURL, title, artist, videoID, thumbnailURL, addToQueue, isLive, expireDate);
break;
case "PlayNext":
@@ -523,22 +551,22 @@ namespace MusicApp.Resources.Portable_Class
return;
}
Video info = await client.GetVideoAsync(videoID);
thumbnailURL = info.Thumbnails.HighResUrl;
if (artist == null || artist == "")
artist = info.Author;
Console.WriteLine("&Parse and play current index edit");
Console.WriteLine("&Queue Count: " + queue.Count);
Console.WriteLine("&CurrentID: " + CurrentID());
queue[CurrentID()].Album = thumbnailURL;
queue[CurrentID()].Artist = artist;
if (!isLive)
Console.WriteLine("&Action skipped");
if (!UseCastPlayer)
{
DateTimeOffset expireDate = mediaStreamInfo.ValidUntil;
queue[CurrentID()].ExpireDate = expireDate;
Video info = await client.GetVideoAsync(videoID);
thumbnailURL = info.Thumbnails.HighResUrl;
if (artist == null || artist == "")
artist = info.Author;
queue[CurrentID()].Album = thumbnailURL;
queue[CurrentID()].Artist = artist;
if (!isLive)
{
expireDate = mediaStreamInfo.ValidUntil;
queue[CurrentID()].ExpireDate = expireDate;
}
}
}
catch (System.Net.Http.HttpRequestException)
@@ -558,15 +586,16 @@ namespace MusicApp.Resources.Portable_Class
// return;
//}
UpdateQueueItemDB(queue[CurrentID()]);
Player.instance?.RefreshPlayer();
Console.WriteLine("&Catch block exited");
Player.instance?.RefreshPlayer();
if (MainActivity.instance != null)
{
MainActivity.instance.FindViewById<ProgressBar>(Resource.Id.ytProgress).Visibility = ViewStates.Gone;
MainActivity.instance.ShowSmallPlayer();
MainActivity.instance.ShowPlayer();
}
UpdateQueueItemDB(await GetItem());
parsing = false;
}
}
@@ -1490,7 +1519,6 @@ namespace MusicApp.Resources.Portable_Class
if (UseCastPlayer)
{
Console.WriteLine("&Getting queue from cast");
Console.WriteLine("&Count: " + RemotePlayer?.MediaQueue.ItemCount);
if (RemotePlayer?.MediaStatus?.QueueItems.Count == 0)
{
@@ -1500,44 +1528,34 @@ namespace MusicApp.Resources.Portable_Class
if(RemotePlayer.CurrentItem != null)
currentID = RemotePlayer.MediaQueue.IndexOfItemWithId(RemotePlayer.CurrentItem.ItemId);
else
currentID = -1;
Console.WriteLine("&CurentID: " + currentID);
bool showPlayer = queue.Count == 0;
queue.Clear();
for (int i = 0; i < RemotePlayer.MediaQueue.ItemCount; i++)
queue.Add((Song)RemotePlayer.MediaQueue.GetItemAtIndex(i, i == currentID || i == currentID + 1));
queue.Add((Song)RemotePlayer.MediaQueue.GetItemAtIndex(i, true));
if(currentID != -1)
{
while (queue[currentID] == null || Player.instance == null)
await Task.Delay(1000);
}
foreach (Song song in queue)
Console.WriteLine("&Song: " + song?.Title);
Intent intent = new Intent(MainActivity.instance, typeof(MusicPlayer));
intent.SetAction("CastListener");
MainActivity.instance.StartService(intent);
Console.WriteLine("&Waiting for fetch - queue count: " + queue.Count);
if (queue.Count > 0)
{
if (currentID != -1)
{
while (currentID >= queue.Count || queue[currentID] == null || Player.instance == null)
await Task.Delay(1000);
}
Console.WriteLine("&Fetched");
foreach (Song song in queue)
Console.WriteLine("&Song: " + song?.Title);
Intent intent = new Intent(MainActivity.instance, typeof(MusicPlayer));
intent.SetAction("CastListener");
MainActivity.instance.StartService(intent);
Home.instance?.AddQueue();
Home.instance?.RefreshQueue();
Queue.instance?.Refresh();
if (showPlayer)
MainActivity.instance.ShowSmallPlayer();
else
{
MainActivity.instance.FindViewById(Resource.Id.playerSheet).Visibility = ViewStates.Visible;
MainActivity.instance.FindViewById<FrameLayout>(Resource.Id.contentView).SetPadding(0, 0, 0, MainActivity.instance.DpToPx(70));
Player.instance.RefreshPlayer();
}
MainActivity.instance.ShowSmallPlayer();
}
}
}
@@ -80,10 +80,14 @@ namespace MusicApp.Resources.Portable_Class
public void Refresh()
{
if (!MusicPlayer.UseCastPlayer)
adapter.UpdateList(MusicPlayer.queue);
else
{
adapter.NotifyDataSetChanged();
MusicPlayer.RemotePlayer.MediaQueue.RegisterCallback(new QueueCallback(adapter));
}
}
public void RefreshCurrent()