getting youtube playlists

This commit is contained in:
Gboy9155
2017-11-15 20:00:19 +01:00
parent f0bdbfee0a
commit cfb5c2eb91
5 changed files with 145 additions and 30 deletions

View File

@@ -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<ImageButton>(Resource.Id.spPlay).SetImageResource(Resource.Drawable.ic_pause_black_24dp);
else
smallPlayer.FindViewById<ImageButton>(Resource.Id.spPlay).SetImageResource(Resource.Drawable.ic_play_arrow_black_24dp);
if (!prepared)
{
smallPlayer.FindViewById<ImageButton>(Resource.Id.spLast).Click += Last_Click;

View File

@@ -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<string> 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);
}
}

View File

@@ -88,16 +88,21 @@ namespace MusicApp.Resources.Portable_Class
artist.Selected = true;
artist.SetMarqueeRepeatLimit(3);
if(current.GetAlbum() == null)
if (MusicPlayer.isRunning)
playerView.FindViewById<ImageButton>(Resource.Id.playButton).SetImageResource(Resource.Drawable.ic_pause_black_24dp);
else
playerView.FindViewById<ImageButton>(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<SeekBar>(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<TextView>(Resource.Id.nextArtist).Text = "Nothing.";
ImageView nextArt = playerView.FindViewById<ImageView>(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<ImageButton>(Resource.Id.playButton).SetImageResource(Resource.Drawable.ic_pause_black_24dp);
else
playerView.FindViewById<ImageButton>(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<TextView>(Resource.Id.nextArtist).Text = "Nothing.";
ImageView nextArt = playerView.FindViewById<ImageView>(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)

View File

@@ -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<Song> ytList = new List<Song>();
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;

View File

@@ -20,14 +20,14 @@ namespace MusicApp.Resources.Portable_Class
public class YoutubeEngine : ListFragment
{
public static YoutubeEngine instance;
public static YouTubeService youtubeService;
public static List<Song> 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(() =>
{