mirror of
https://github.com/zoriya/Opus.git
synced 2025-12-06 06:26:15 +00:00
youtube player works
This commit is contained in:
@@ -147,8 +147,8 @@ namespace MusicApp
|
||||
PlaylistTracks.instance.result = null;
|
||||
if (FolderTracks.instance != null)
|
||||
FolderTracks.instance.result = null;
|
||||
if (YoutubeEngine.instance != null)
|
||||
YoutubeEngine.instance.result = null;
|
||||
//if (YoutubeEngine.instance != null)
|
||||
// YoutubeEngine.result = null;
|
||||
}
|
||||
|
||||
public void HideSearch()
|
||||
@@ -286,10 +286,17 @@ namespace MusicApp
|
||||
smallPlayer.FindViewById<TextView>(Resource.Id.spArtist).Text = current.GetArtist();
|
||||
ImageView art = smallPlayer.FindViewById<ImageView>(Resource.Id.spArt);
|
||||
|
||||
var songCover = Android.Net.Uri.Parse("content://media/external/audio/albumart");
|
||||
var songAlbumArtUri = ContentUris.WithAppendedId(songCover, current.GetAlbumArt());
|
||||
if (current.IsYt)
|
||||
{
|
||||
Picasso.With(Application.Context).Load(current.GetAlbum()).Placeholder(Resource.Drawable.MusicIcon).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).Into(art);
|
||||
}
|
||||
|
||||
SetSmallPlayerProgressBar();
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
Title = { Text = songList[position].GetName() },
|
||||
Artist = { Text = songList[position].GetArtist() },
|
||||
};
|
||||
if(songList[position].GetAlbumArt() == -1)
|
||||
if(songList[position].GetAlbumArt() == -1 || songList[position].IsYt)
|
||||
{
|
||||
var songAlbumArtUri = Android.Net.Uri.Parse(songList[position].GetAlbum());
|
||||
Picasso.With(Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(holder.AlbumArt);
|
||||
@@ -49,8 +49,6 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
Picasso.With(Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(holder.AlbumArt);
|
||||
}
|
||||
|
||||
//convertView.SetBackgroundColor(Color.White);
|
||||
return convertView;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,9 +12,6 @@ using Android.Content.PM;
|
||||
using Android.Support.V4.App;
|
||||
using Android.Support.V7.App;
|
||||
|
||||
using EventArgs = System.EventArgs;
|
||||
using Square.Picasso;
|
||||
|
||||
namespace MusicApp.Resources.Portable_Class
|
||||
{
|
||||
public class Browse : ListFragment
|
||||
|
||||
@@ -76,10 +76,26 @@ namespace MusicApp.Resources.Portable_Class
|
||||
break;
|
||||
|
||||
case "PlayNext":
|
||||
string title = intent.GetStringExtra("title");
|
||||
if (title != null)
|
||||
{
|
||||
string artist = intent.GetStringExtra("artist");
|
||||
string thumbnailURI = intent.GetStringExtra("thumbnailURI");
|
||||
AddToQueue(file, title, artist, thumbnailURI);
|
||||
return StartCommandResult.Sticky;
|
||||
}
|
||||
AddToQueue(file);
|
||||
break;
|
||||
|
||||
case "PlayLast":
|
||||
title = intent.GetStringExtra("title");
|
||||
if (title != null)
|
||||
{
|
||||
string artist = intent.GetStringExtra("artist");
|
||||
string thumbnailURI = intent.GetStringExtra("thumbnailURI");
|
||||
PlayLastInQueue(file, title, artist, thumbnailURI);
|
||||
return StartCommandResult.Sticky;
|
||||
}
|
||||
PlayLastInQueue(file);
|
||||
break;
|
||||
|
||||
@@ -123,7 +139,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
player.AddListener(this);
|
||||
}
|
||||
|
||||
public void Play(string filePath, string title = null, string artist = null, string thumbnailURI = null)
|
||||
public void Play(string filePath, string title = null, string artist = null, string thumbnailURI = null, bool addToQueue = true)
|
||||
{
|
||||
isRunning = true;
|
||||
if (player == null)
|
||||
@@ -155,11 +171,12 @@ namespace MusicApp.Resources.Portable_Class
|
||||
GetTrackSong(filePath, out song);
|
||||
else
|
||||
{
|
||||
song = new Song(title, artist, thumbnailURI, -1, -1, filePath);
|
||||
song = new Song(title, artist, thumbnailURI, -1, -1, filePath, true);
|
||||
}
|
||||
CreateNotification(song.GetName(), song.GetArtist(), song.GetAlbumArt(), song.GetAlbum());
|
||||
//queue.Clear();
|
||||
AddToQueue(song);
|
||||
|
||||
if(addToQueue)
|
||||
AddToQueue(song);
|
||||
}
|
||||
|
||||
public async void RandomPlay(List<string> filePath)
|
||||
@@ -177,9 +194,13 @@ namespace MusicApp.Resources.Portable_Class
|
||||
}
|
||||
}
|
||||
|
||||
public void AddToQueue(string filePath)
|
||||
public void AddToQueue(string filePath, string title = null, string artist = null, string thumbnailURI = null)
|
||||
{
|
||||
GetTrackSong(filePath, out Song song);
|
||||
Song song = null;
|
||||
if(title == null)
|
||||
GetTrackSong(filePath, out song);
|
||||
else
|
||||
song = new Song(title, artist, thumbnailURI, -1, -1, filePath, true);
|
||||
if (CurrentID() == -1)
|
||||
queue.Add(song);
|
||||
else
|
||||
@@ -200,6 +221,12 @@ namespace MusicApp.Resources.Portable_Class
|
||||
queue.Add(song);
|
||||
}
|
||||
|
||||
public void PlayLastInQueue(string filePath, string title, string artist, string thumbnailURI)
|
||||
{
|
||||
Song song = new Song(title, artist, thumbnailURI, -1, -1, filePath, true);
|
||||
queue.Add(song);
|
||||
}
|
||||
|
||||
public void PlayLast()
|
||||
{
|
||||
if (CurrentID() - 1 < 0)
|
||||
@@ -225,7 +252,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
void SwitchQueue(string filePath)
|
||||
{
|
||||
Play(filePath);
|
||||
Play(filePath, null, null, null, false);
|
||||
|
||||
GetTrackSong(filePath, out Song song);
|
||||
|
||||
@@ -237,7 +264,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
smallPlayer.FindViewById<TextView>(Resource.Id.spArtist).Text = song.GetArtist();
|
||||
ImageView art = smallPlayer.FindViewById<ImageView>(Resource.Id.spArt);
|
||||
|
||||
if(song.GetAlbum() == null)
|
||||
if(!song.IsYt)
|
||||
{
|
||||
var songCover = Uri.Parse("content://media/external/audio/albumart");
|
||||
var nextAlbumArtUri = ContentUris.WithAppendedId(songCover, song.GetAlbumArt());
|
||||
|
||||
@@ -3,66 +3,68 @@ using Android.Content;
|
||||
using Android.OS;
|
||||
using Android.Views;
|
||||
using Android.Widget;
|
||||
using Android;
|
||||
using Android.Content.PM;
|
||||
using Android.Support.Design.Widget;
|
||||
using Google.Apis.YouTube.v3;
|
||||
using Android.Gms.Auth.Api.SignIn;
|
||||
using Android.Gms.Common.Apis;
|
||||
using Android.Gms.Common;
|
||||
using Android.Gms.Auth.Api;
|
||||
using System.Collections.Generic;
|
||||
using Google.Apis.Services;
|
||||
using Android.Accounts;
|
||||
using Android.Preferences;
|
||||
using System;
|
||||
using YoutubeExplode;
|
||||
using System.Linq;
|
||||
using MusicApp.Resources.values;
|
||||
using System.Threading.Tasks;
|
||||
using Android.Support.V7.App;
|
||||
|
||||
namespace MusicApp.Resources.Portable_Class
|
||||
{
|
||||
public class YoutubeEngine : ListFragment
|
||||
{
|
||||
public static YoutubeEngine instance;
|
||||
public List<YtFile> result;
|
||||
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;
|
||||
private string videoID;
|
||||
//private string oAuthKey;
|
||||
//private GoogleApiClient googleClient;
|
||||
//private const int signPickerID = 9001;
|
||||
//private const int playServiceID = 9002;
|
||||
//private const string accountName = "accountName";
|
||||
|
||||
|
||||
//private static readonly string[] permissions = new string[]
|
||||
//{
|
||||
// Manifest.Permission.GetAccounts
|
||||
//};
|
||||
|
||||
public override void OnActivityCreated(Bundle savedInstanceState)
|
||||
{
|
||||
base.OnActivityCreated(savedInstanceState);
|
||||
|
||||
emptyView = LayoutInflater.Inflate(Resource.Layout.DownloadLayout, null);
|
||||
ListView.EmptyView = emptyView;
|
||||
Activity.AddContentView(emptyView, View.LayoutParameters);
|
||||
ListAdapter = null;
|
||||
ListView.ItemClick += ListView_ItemClick;
|
||||
ListView.ItemLongClick += ListView_ItemLongClick;
|
||||
|
||||
if (youtubeService == null)
|
||||
emptyView = LayoutInflater.Inflate(Resource.Layout.DownloadLayout, null);
|
||||
ListView.EmptyView = emptyView;
|
||||
if(result != null)
|
||||
{
|
||||
youtubeService = new YouTubeService(new BaseClientService.Initializer()
|
||||
{
|
||||
ApiKey = ApiKey,
|
||||
ApplicationName = "MusicApp"
|
||||
});
|
||||
ListAdapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
ListAdapter = null;
|
||||
Activity.AddContentView(emptyView, View.LayoutParameters);
|
||||
}
|
||||
|
||||
CreateYoutube();
|
||||
}
|
||||
|
||||
private async void CreateYoutube()
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
if (youtubeService == null)
|
||||
{
|
||||
youtubeService = new YouTubeService(new BaseClientService.Initializer()
|
||||
{
|
||||
ApiKey = ApiKey,
|
||||
ApplicationName = "MusicApp"
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public override void OnDestroy()
|
||||
@@ -88,121 +90,6 @@ namespace MusicApp.Resources.Portable_Class
|
||||
return instance;
|
||||
}
|
||||
|
||||
#region AccountChooser
|
||||
//void CheckInitializationState()
|
||||
//{
|
||||
// if (!GooglePlayServiceAvailable())
|
||||
// GetGooglePlayServices();
|
||||
// else if (oAuthKey == null)
|
||||
// ChooseAccount();
|
||||
// else
|
||||
// GetPermissions(permissions);
|
||||
//}
|
||||
|
||||
//private bool GooglePlayServiceAvailable()
|
||||
//{
|
||||
// GoogleApiAvailability availability = GoogleApiAvailability.Instance;
|
||||
// int result = availability.IsGooglePlayServicesAvailable(Android.App.Application.Context);
|
||||
// return result == ConnectionResult.Success;
|
||||
//}
|
||||
|
||||
//private void GetGooglePlayServices()
|
||||
//{
|
||||
// GoogleApiAvailability availability = GoogleApiAvailability.Instance;
|
||||
// int result = availability.IsGooglePlayServicesAvailable(Android.App.Application.Context);
|
||||
// if(availability.IsUserResolvableError(result))
|
||||
// availability.GetErrorDialog(MainActivity.instance, result, playServiceID).Show();
|
||||
//}
|
||||
|
||||
//void ChooseAccount()
|
||||
//{
|
||||
// bool hasPermissions = CheckPermissions(permissions);
|
||||
// if (hasPermissions)
|
||||
// {
|
||||
// string account = Activity.GetPreferences(FileCreationMode.Private).GetString(accountName, null);
|
||||
// oAuthKey = Activity.GetPreferences(FileCreationMode.Private).GetString("oAuth", null);
|
||||
// if (account != null)
|
||||
// {
|
||||
// CheckInitializationState();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn).RequestEmail().Build();
|
||||
// googleClient = new GoogleApiClient.Builder(Android.App.Application.Context).EnableAutoManage(Activity, (result) => { }).AddApi(Auth.GOOGLE_SIGN_IN_API, gso).Build();
|
||||
// Intent intent = Auth.GoogleSignInApi.GetSignInIntent(googleClient);
|
||||
// StartActivityForResult(intent, signPickerID);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// GetPermissions(permissions);
|
||||
// }
|
||||
//}
|
||||
|
||||
//public override void OnActivityResult(int requestCode, int resultCode, Intent data)
|
||||
//{
|
||||
// base.OnActivityResult(requestCode, resultCode, data);
|
||||
// if(requestCode == signPickerID)
|
||||
// {
|
||||
// GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
|
||||
// if (result.IsSuccess)
|
||||
// {
|
||||
// GoogleSignInAccount signInAccount = result.SignInAccount;
|
||||
// Account account = signInAccount.Account;
|
||||
// ISharedPreferences settings = Activity.GetPreferences(FileCreationMode.Private);
|
||||
// ISharedPreferencesEditor editor = settings.Edit();
|
||||
// editor.PutString(accountName, signInAccount.DisplayName);
|
||||
// editor.Apply();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region Permissions
|
||||
//void GetPermissions(string[] permissions)
|
||||
//{
|
||||
// bool hasPermissions = CheckPermissions(permissions);
|
||||
|
||||
// if (!hasPermissions)
|
||||
// RequestPermissions(permissions, 0);
|
||||
// //else
|
||||
// // PopulateList();
|
||||
//}
|
||||
|
||||
//bool CheckPermissions(string[] permissions)
|
||||
//{
|
||||
// bool hasPermissions = true;
|
||||
// foreach (string permission in permissions)
|
||||
// {
|
||||
// if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(Android.App.Application.Context, permission) != (int)Permission.Granted)
|
||||
// {
|
||||
// hasPermissions = false;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// return hasPermissions;
|
||||
//}
|
||||
|
||||
//public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
|
||||
//{
|
||||
// switch (requestCode)
|
||||
// {
|
||||
// case 0:
|
||||
// {
|
||||
// if (grantResults[0] == Permission.Granted)
|
||||
// {
|
||||
// PopulateList();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Snackbar.Make(View, "Permission denied, can't list search on youtube.", Snackbar.LengthShort).Show();
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
#endregion
|
||||
|
||||
public async void Search(string search)
|
||||
{
|
||||
if (search == null || search == "")
|
||||
@@ -216,20 +103,55 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
var searchReponse = await searchResult.ExecuteAsync();
|
||||
|
||||
result = new List<YtFile>();
|
||||
result = new List<Song>();
|
||||
|
||||
foreach(var video in searchReponse.Items)
|
||||
{
|
||||
YtFile videoInfo = new YtFile(video.Snippet.Title, video.Snippet.ChannelTitle, video.Id.VideoId, video.Snippet.Thumbnails.Default__.Url);
|
||||
Song videoInfo = new Song(video.Snippet.Title, video.Snippet.ChannelTitle, video.Snippet.Thumbnails.Default__.Url, -1, -1, video.Id.VideoId, true);
|
||||
result.Add(videoInfo);
|
||||
}
|
||||
|
||||
ListAdapter = new YtAdapter(Android.App.Application.Context, Resource.Layout.YtList, result);
|
||||
ListAdapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, result);
|
||||
}
|
||||
|
||||
private async void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
|
||||
private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
|
||||
{
|
||||
videoID = result[e.Position].GetPath();
|
||||
Play(videoID);
|
||||
}
|
||||
|
||||
private void ListView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
|
||||
{
|
||||
Song item = result[e.Position];
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(Activity, Resource.Style.AppCompatAlertDialogStyle);
|
||||
builder.SetTitle("Pick an action");
|
||||
builder.SetItems(actions, (senderAlert, args) =>
|
||||
{
|
||||
switch (args.Which)
|
||||
{
|
||||
case 0:
|
||||
Play(item.GetPath());
|
||||
break;
|
||||
case 1:
|
||||
PlayNext(item.GetPath());
|
||||
break;
|
||||
case 2:
|
||||
PlayLast(item.GetPath());
|
||||
break;
|
||||
case 3:
|
||||
Download(item.GetName(), item.GetPath());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
builder.Show();
|
||||
|
||||
}
|
||||
|
||||
private async void Play(string videoID)
|
||||
{
|
||||
videoID = result[e.Position].Id;
|
||||
var client = new YoutubeClient();
|
||||
var videoInfo = await client.GetVideoInfoAsync(videoID);
|
||||
var streamInfo = videoInfo.AudioStreams.OrderBy(s => s.Bitrate).Last();
|
||||
@@ -240,9 +162,43 @@ namespace MusicApp.Resources.Portable_Class
|
||||
intent.PutExtra("artist", videoInfo.Author.Title);
|
||||
intent.PutExtra("thumbnailURI", videoInfo.ImageThumbnailUrl);
|
||||
Android.App.Application.Context.StartService(intent);
|
||||
|
||||
MainActivity.instance.HideTabs();
|
||||
MainActivity.instance.HideSearch();
|
||||
MainActivity.instance.SupportFragmentManager.BeginTransaction().Replace(Resource.Id.contentView, Player.NewInstance()).Commit();
|
||||
}
|
||||
|
||||
private void ListView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
|
||||
private async void PlayNext(string videoID)
|
||||
{
|
||||
var client = new YoutubeClient();
|
||||
var videoInfo = await client.GetVideoInfoAsync(videoID);
|
||||
var streamInfo = videoInfo.AudioStreams.OrderBy(s => s.Bitrate).Last();
|
||||
|
||||
Intent intent = new Intent(Android.App.Application.Context, typeof(MusicPlayer));
|
||||
intent.SetAction("PlayNext");
|
||||
intent.PutExtra("file", streamInfo.Url);
|
||||
intent.PutExtra("title", videoInfo.Title);
|
||||
intent.PutExtra("artist", videoInfo.Author.Title);
|
||||
intent.PutExtra("thumbnailURI", videoInfo.ImageThumbnailUrl);
|
||||
Android.App.Application.Context.StartService(intent);
|
||||
}
|
||||
|
||||
private async void PlayLast(string videoID)
|
||||
{
|
||||
var client = new YoutubeClient();
|
||||
var videoInfo = await client.GetVideoInfoAsync(videoID);
|
||||
var streamInfo = videoInfo.AudioStreams.OrderBy(s => s.Bitrate).Last();
|
||||
|
||||
Intent intent = new Intent(Android.App.Application.Context, typeof(MusicPlayer));
|
||||
intent.SetAction("PlayLast");
|
||||
intent.PutExtra("file", streamInfo.Url);
|
||||
intent.PutExtra("title", videoInfo.Title);
|
||||
intent.PutExtra("artist", videoInfo.Author.Title);
|
||||
intent.PutExtra("thumbnailURI", videoInfo.ImageThumbnailUrl);
|
||||
Android.App.Application.Context.StartService(intent);
|
||||
}
|
||||
|
||||
private void Download(string name, string videoID)
|
||||
{
|
||||
ISharedPreferences prefManager = PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context);
|
||||
if (prefManager.GetString("downloadPath", null) != null)
|
||||
@@ -250,9 +206,9 @@ namespace MusicApp.Resources.Portable_Class
|
||||
Toast.MakeText(Android.App.Application.Context, "Downloading...", ToastLength.Short).Show();
|
||||
Context context = Android.App.Application.Context;
|
||||
Intent intent = new Intent(context, typeof(Downloader));
|
||||
intent.PutExtra("videoID", result[e.Position].Id);
|
||||
intent.PutExtra("videoID", videoID);
|
||||
intent.PutExtra("path", prefManager.GetString("downloadPath", null));
|
||||
intent.PutExtra("name", result[e.Position].Title);
|
||||
intent.PutExtra("name", name);
|
||||
context.StartService(intent);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
private long AlbumArt;
|
||||
private long id;
|
||||
private string path;
|
||||
private bool isYT;
|
||||
|
||||
public string GetName() { return Name; }
|
||||
public string GetArtist() { return Artist; }
|
||||
@@ -16,8 +17,9 @@
|
||||
public long GetAlbumArt() { return AlbumArt; }
|
||||
public long GetID() { return id; }
|
||||
public string GetPath() { return path; }
|
||||
public bool IsYt { get { return isYT; } }
|
||||
|
||||
public Song(string Name, string Artist, string Album, long AlbumArt, long id, string path)
|
||||
public Song(string Name, string Artist, string Album, long AlbumArt, long id, string path, bool isYT = false)
|
||||
{
|
||||
this.Name = Name;
|
||||
this.Artist = Artist;
|
||||
@@ -25,6 +27,7 @@
|
||||
this.AlbumArt = AlbumArt;
|
||||
this.id = id;
|
||||
this.path = path;
|
||||
this.isYT = isYT;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user