From cfb5c2eb91b2f63a20f00aa6811f84ec2753f66c Mon Sep 17 00:00:00 2001 From: Gboy9155 <32224410+Gboy9155@users.noreply.github.com> Date: Wed, 15 Nov 2017 20:00:19 +0100 Subject: [PATCH] getting youtube playlists --- MusicApp/MainActivity.cs | 9 ++- .../Resources/Portable Class/MusicPlayer.cs | 52 +++++++++++---- MusicApp/Resources/Portable Class/Player.cs | 44 +++++++++---- MusicApp/Resources/Portable Class/Playlist.cs | 64 +++++++++++++++++++ .../Resources/Portable Class/YoutubeEngine.cs | 6 +- 5 files changed, 145 insertions(+), 30 deletions(-) diff --git a/MusicApp/MainActivity.cs b/MusicApp/MainActivity.cs index cc0ce19..57c3bf1 100644 --- a/MusicApp/MainActivity.cs +++ b/MusicApp/MainActivity.cs @@ -288,18 +288,23 @@ namespace MusicApp if (current.IsYt) { - Picasso.With(Application.Context).Load(current.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Into(art); + Picasso.With(Application.Context).Load(current.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(art); } else { var songCover = Android.Net.Uri.Parse("content://media/external/audio/albumart"); var songAlbumArtUri = ContentUris.WithAppendedId(songCover, current.GetAlbumArt()); - Picasso.With(Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Into(art); + Picasso.With(Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(art); } SetSmallPlayerProgressBar(); + if (MusicPlayer.isRunning) + smallPlayer.FindViewById(Resource.Id.spPlay).SetImageResource(Resource.Drawable.ic_pause_black_24dp); + else + smallPlayer.FindViewById(Resource.Id.spPlay).SetImageResource(Resource.Drawable.ic_play_arrow_black_24dp); + if (!prepared) { smallPlayer.FindViewById(Resource.Id.spLast).Click += Last_Click; diff --git a/MusicApp/Resources/Portable Class/MusicPlayer.cs b/MusicApp/Resources/Portable Class/MusicPlayer.cs index c5eb01d..fa168e9 100644 --- a/MusicApp/Resources/Portable Class/MusicPlayer.cs +++ b/MusicApp/Resources/Portable Class/MusicPlayer.cs @@ -99,9 +99,6 @@ namespace MusicApp.Resources.Portable_Class PlayLastInQueue(file); break; - case "QueueSwitch": - SwitchQueue(file); - break; case "Stop": if(isRunning) Stop(); @@ -179,6 +176,38 @@ namespace MusicApp.Resources.Portable_Class AddToQueue(song); } + public void Play(Song song, bool addToQueue = true) + { + isRunning = true; + if (player == null) + InitializeService(); + + if (mediaSession == null) + { + mediaSession = new MediaSessionCompat(Application.Context, "MusicApp"); + mediaSession.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons | MediaSessionCompat.FlagHandlesTransportControls); + PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder().SetActions(PlaybackStateCompat.ActionPlay | PlaybackStateCompat.ActionPause); + mediaSession.SetPlaybackState(builder.Build()); + } + + DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(Application.Context, "MusicApp"); + IExtractorsFactory extractorFactory = new DefaultExtractorsFactory(); + Handler handler = new Handler(); + IMediaSource mediaSource = new ExtractorMediaSource(Uri.Parse(song.GetPath()), dataSourceFactory, extractorFactory, handler, null); + var audioFocus = audioManager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain); + if (audioFocus != AudioFocusRequest.Granted) + { + Console.WriteLine("Can't Get Audio Focus"); + return; + } + player.PlayWhenReady = true; + player.Prepare(mediaSource, true, true); + CreateNotification(song.GetName(), song.GetArtist(), song.GetAlbumArt(), song.GetAlbum()); + + if (addToQueue) + AddToQueue(song); + } + public async void RandomPlay(List filePath) { Random r = new Random(); @@ -233,8 +262,7 @@ namespace MusicApp.Resources.Portable_Class return; Song last = queue[CurrentID() - 1]; - string filePath = last.GetPath(); - SwitchQueue(filePath); + SwitchQueue(last); } public void PlayNext() @@ -246,15 +274,12 @@ namespace MusicApp.Resources.Portable_Class } Song next = queue[CurrentID() + 1]; - string filePath = next.GetPath(); - SwitchQueue(filePath); + SwitchQueue(next); } - void SwitchQueue(string filePath) + void SwitchQueue(Song song) { - Play(filePath, null, null, null, false); - - GetTrackSong(filePath, out Song song); + Play(song, false); if (Player.instance != null) Player.instance.RefreshPlayer(); @@ -269,11 +294,12 @@ namespace MusicApp.Resources.Portable_Class var songCover = Uri.Parse("content://media/external/audio/albumart"); var nextAlbumArtUri = ContentUris.WithAppendedId(songCover, song.GetAlbumArt()); - Picasso.With(Application.Context).Load(nextAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Into(art); + Picasso.With(Application.Context).Load(nextAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(art); } else { - Picasso.With(Application.Context).Load(song.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Into(art); + Console.WriteLine("Yt song: " + song.GetAlbum()); + Picasso.With(Application.Context).Load(song.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(art); } } diff --git a/MusicApp/Resources/Portable Class/Player.cs b/MusicApp/Resources/Portable Class/Player.cs index eb5ca2e..0b78236 100644 --- a/MusicApp/Resources/Portable Class/Player.cs +++ b/MusicApp/Resources/Portable Class/Player.cs @@ -88,16 +88,21 @@ namespace MusicApp.Resources.Portable_Class artist.Selected = true; artist.SetMarqueeRepeatLimit(3); - if(current.GetAlbum() == null) + if (MusicPlayer.isRunning) + playerView.FindViewById(Resource.Id.playButton).SetImageResource(Resource.Drawable.ic_pause_black_24dp); + else + playerView.FindViewById(Resource.Id.playButton).SetImageResource(Resource.Drawable.ic_play_arrow_black_24dp); + + if (current.GetAlbum() == null) { var songCover = Android.Net.Uri.Parse("content://media/external/audio/albumart"); var songAlbumArtUri = ContentUris.WithAppendedId(songCover, current.GetAlbumArt()); - Picasso.With(Android.App.Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Into(imgView); + Picasso.With(Android.App.Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(imgView); } else { - Picasso.With(Android.App.Application.Context).Load(current.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Into(imgView); + Picasso.With(Android.App.Application.Context).Load(current.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(imgView); } bar = playerView.FindViewById(Resource.Id.songTimer); @@ -117,11 +122,11 @@ namespace MusicApp.Resources.Portable_Class var songCover = Android.Net.Uri.Parse("content://media/external/audio/albumart"); var nextAlbumArtUri = ContentUris.WithAppendedId(songCover, next.GetAlbumArt()); - Picasso.With(Android.App.Application.Context).Load(nextAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Into(nextArt); + Picasso.With(Android.App.Application.Context).Load(nextAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(nextArt); } else { - Picasso.With(Android.App.Application.Context).Load(next.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Into(nextArt); + Picasso.With(Android.App.Application.Context).Load(next.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(nextArt); } } else @@ -130,8 +135,13 @@ namespace MusicApp.Resources.Portable_Class playerView.FindViewById(Resource.Id.nextArtist).Text = "Nothing."; ImageView nextArt = playerView.FindViewById(Resource.Id.nextArt); - Picasso.With(Android.App.Application.Context).Load(Resource.Drawable.noAlbum).Placeholder(Resource.Drawable.MusicIcon).Into(nextArt); + Picasso.With(Android.App.Application.Context).Load(Resource.Drawable.noAlbum).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(nextArt); } + + while (MusicPlayer.player.Duration < 1) + await Task.Delay(100); + + bar.Max = (int)MusicPlayer.player.Duration; } public async void RefreshPlayer() @@ -148,16 +158,21 @@ namespace MusicApp.Resources.Portable_Class title.Text = current.GetName(); artist.Text = current.GetArtist(); - if (current.GetAlbum() == null) + if (MusicPlayer.isRunning) + playerView.FindViewById(Resource.Id.playButton).SetImageResource(Resource.Drawable.ic_pause_black_24dp); + else + playerView.FindViewById(Resource.Id.playButton).SetImageResource(Resource.Drawable.ic_play_arrow_black_24dp); + + if (!current.IsYt) { var songCover = Android.Net.Uri.Parse("content://media/external/audio/albumart"); var songAlbumArtUri = ContentUris.WithAppendedId(songCover, current.GetAlbumArt()); - Picasso.With(Android.App.Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Into(imgView); + Picasso.With(Android.App.Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(imgView); } else { - Picasso.With(Android.App.Application.Context).Load(current.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Into(imgView); + Picasso.With(Android.App.Application.Context).Load(current.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(imgView); } bool asNext = MusicPlayer.queue.Count > MusicPlayer.CurrentID() + 1; @@ -173,11 +188,11 @@ namespace MusicApp.Resources.Portable_Class var songCover = Android.Net.Uri.Parse("content://media/external/audio/albumart"); var nextAlbumArtUri = ContentUris.WithAppendedId(songCover, next.GetAlbumArt()); - Picasso.With(Android.App.Application.Context).Load(nextAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Into(nextArt); + Picasso.With(Android.App.Application.Context).Load(nextAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(nextArt); } else { - Picasso.With(Android.App.Application.Context).Load(next.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Into(nextArt); + Picasso.With(Android.App.Application.Context).Load(next.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(nextArt); } } else @@ -186,8 +201,13 @@ namespace MusicApp.Resources.Portable_Class playerView.FindViewById(Resource.Id.nextArtist).Text = "Nothing."; ImageView nextArt = playerView.FindViewById(Resource.Id.nextArt); - Picasso.With(Android.App.Application.Context).Load(Resource.Drawable.noAlbum).Placeholder(Resource.Drawable.MusicIcon).Into(nextArt); + Picasso.With(Android.App.Application.Context).Load(Resource.Drawable.noAlbum).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(nextArt); } + + while (MusicPlayer.player.Duration < 1) + await Task.Delay(100); + + bar.Max = (int)MusicPlayer.player.Duration; } private void AddToPlaylist_Click(object sender, EventArgs e) diff --git a/MusicApp/Resources/Portable Class/Playlist.cs b/MusicApp/Resources/Portable Class/Playlist.cs index b8a6db8..21d588c 100644 --- a/MusicApp/Resources/Portable Class/Playlist.cs +++ b/MusicApp/Resources/Portable Class/Playlist.cs @@ -11,6 +11,12 @@ using Android.Content.PM; using Android.Support.Design.Widget; using Android; using Android.Net; +using System.Threading.Tasks; +using Java.Util; +using Google.Apis.YouTube.v3; +using Google.Apis.YouTube.v3.Data; +using MusicApp.Resources.values; +using static Android.Provider.MediaStore.Audio; namespace MusicApp.Resources.Portable_Class { @@ -35,6 +41,14 @@ namespace MusicApp.Resources.Portable_Class if(ListView.Adapter == null) GetStoragePermission(); + + //if(GoogleAccount != null) + { + if (YoutubeEngine.youtubeService == null) + YoutubeEngine.CreateYoutube(); + + GetYoutubePlaylists(); + } } public override void OnDestroy() @@ -130,6 +144,56 @@ namespace MusicApp.Resources.Portable_Class } } + async void GetYoutubePlaylists() + { + while (YoutubeEngine.youtubeService == null) + await Task.Delay(100); + + HashMap parameters = new HashMap(); + parameters.Put("part", "snippet,contentDetails"); + parameters.Put("mine", "true"); + parameters.Put("maxResults", "25"); + parameters.Put("onBehalfOfContentOwner", ""); + parameters.Put("onBehalfOfContentOwnerChannel", ""); + + YouTubeService youtube = YoutubeEngine.youtubeService; + + PlaylistsResource.ListRequest ytPlaylists = youtube.Playlists.List(parameters.Get("part").ToString()); + + if (parameters.ContainsKey("mine") && parameters.Get("mine").ToString() != "") + { + bool mine = (parameters.Get("mine").ToString() == "true") ? true : false; + ytPlaylists.Mine = mine; + } + + if (parameters.ContainsKey("maxResults")) + { + ytPlaylists.MaxResults = long.Parse(parameters.Get("maxResults").ToString()); + } + + if (parameters.ContainsKey("onBehalfOfContentOwner") && parameters.Get("onBehalfOfContentOwner").ToString() != "") + { + ytPlaylists.OnBehalfOfContentOwner = parameters.Get("onBehalfOfContentOwner").ToString(); + } + + if (parameters.ContainsKey("onBehalfOfContentOwnerChannel") && parameters.Get("onBehalfOfContentOwnerChannel").ToString() != "") + { + ytPlaylists.OnBehalfOfContentOwnerChannel = parameters.Get("onBehalfOfContentOwnerChannel").ToString(); + } + + PlaylistListResponse response = await ytPlaylists.ExecuteAsync(); + List ytList = new List(); + + for (int i = 0; i < response.Items.Count ; i++) + { + Google.Apis.YouTube.v3.Data.Playlist playlist = response.Items[i]; + Song song = new Song(playlist.Snippet.Title, playlist.Snippet.ChannelTitle, playlist.Snippet.Thumbnails.Default__.Url, -1, -1, playlist.Id, true); + ytList.Add(song); + } + + Adapter ytAdapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, ytList); + } + private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) { AppCompatActivity act = (AppCompatActivity)Activity; diff --git a/MusicApp/Resources/Portable Class/YoutubeEngine.cs b/MusicApp/Resources/Portable Class/YoutubeEngine.cs index 60bb94c..0931cc2 100644 --- a/MusicApp/Resources/Portable Class/YoutubeEngine.cs +++ b/MusicApp/Resources/Portable Class/YoutubeEngine.cs @@ -20,14 +20,14 @@ namespace MusicApp.Resources.Portable_Class public class YoutubeEngine : ListFragment { public static YoutubeEngine instance; + public static YouTubeService youtubeService; public static List result; private View emptyView; private bool isEmpty = true; private string[] actions = new string[] { "Play", "Play Next", "Play Last", "Download" }; - private string ApiKey = "AIzaSyBOQyZVnBAKjur0ztBuYPSopS725Qudgc4"; - private YouTubeService youtubeService; + public const string ApiKey = "AIzaSyBOQyZVnBAKjur0ztBuYPSopS725Qudgc4"; private string videoID; public override void OnActivityCreated(Bundle savedInstanceState) @@ -52,7 +52,7 @@ namespace MusicApp.Resources.Portable_Class CreateYoutube(); } - private async void CreateYoutube() + public static async void CreateYoutube() { await Task.Run(() => {