mirror of
https://github.com/zoriya/Opus.git
synced 2026-06-08 00:10:28 +00:00
Remaking youtube mixs.
This commit is contained in:
+33
-64
@@ -26,8 +26,10 @@ using MusicApp.Resources.values;
|
||||
using Square.Picasso;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using TagLib;
|
||||
using Xamarin.Auth;
|
||||
using SearchView = Android.Support.V7.Widget.SearchView;
|
||||
|
||||
@@ -87,7 +89,7 @@ namespace MusicApp
|
||||
base.OnCreate(savedInstanceState);
|
||||
|
||||
ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences(this);
|
||||
SwitchTheme(pref.GetInt("theme", 1));
|
||||
SwitchTheme(pref.GetInt("theme", 0));
|
||||
SetContentView(Resource.Layout.Main);
|
||||
instance = this;
|
||||
|
||||
@@ -105,16 +107,18 @@ namespace MusicApp
|
||||
crossToPlay = GetDrawable(Resource.Drawable.CrossToPlay);
|
||||
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
|
||||
NotificationChannel channel = new NotificationChannel("MusicApp.Channel", "Default Channel", NotificationImportance.Low)
|
||||
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
|
||||
{
|
||||
Description = "Channel used for download progress and music control notification.",
|
||||
LockscreenVisibility = NotificationVisibility.Public
|
||||
};
|
||||
channel.EnableVibration(false);
|
||||
channel.EnableLights(false);
|
||||
notificationManager.CreateNotificationChannel(channel);
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
|
||||
NotificationChannel channel = new NotificationChannel("MusicApp.Channel", "Default Channel", NotificationImportance.Low)
|
||||
{
|
||||
Description = "Channel used for download progress and music control notification.",
|
||||
LockscreenVisibility = NotificationVisibility.Public
|
||||
};
|
||||
channel.EnableVibration(false);
|
||||
channel.EnableLights(false);
|
||||
notificationManager.CreateNotificationChannel(channel);
|
||||
}
|
||||
|
||||
if (MusicPlayer.queue.Count > 0)
|
||||
ReCreateSmallPlayer();
|
||||
@@ -1062,13 +1066,28 @@ namespace MusicApp
|
||||
|
||||
private async void YtPlay(object sender, EventArgs e)
|
||||
{
|
||||
if (MusicPlayer.CurrentID() == -1 || !MusicPlayer.queue[MusicPlayer.CurrentID()].IsYt)
|
||||
if (MusicPlayer.CurrentID() == -1 || MusicPlayer.queue[MusicPlayer.CurrentID()].youtubeID == null || MusicPlayer.queue[MusicPlayer.CurrentID()].youtubeID == "")
|
||||
{
|
||||
if (MusicPlayer.CurrentID() != -1)
|
||||
{
|
||||
Stream stream = new FileStream(MusicPlayer.queue[MusicPlayer.CurrentID()].GetPath(), FileMode.Open, FileAccess.Read);
|
||||
|
||||
var meta = TagLib.File.Create(new StreamFileAbstraction(MusicPlayer.queue[MusicPlayer.CurrentID()].GetPath(), stream, stream));
|
||||
string ytID = meta.Tag.Comment;
|
||||
stream.Dispose();
|
||||
|
||||
MusicPlayer.queue[MusicPlayer.CurrentID()].youtubeID = ytID;
|
||||
if (ytID != null && ytID != "")
|
||||
goto YtMix;
|
||||
}
|
||||
|
||||
QuickPlay(this, e);
|
||||
Toast.MakeText(this, "This button create a playlist based on the youtube file your actually watching, it won't work if your not playing a youtube audio", ToastLength.Long).Show();
|
||||
return;
|
||||
}
|
||||
|
||||
YtMix:
|
||||
|
||||
ProgressBar parseProgress = FindViewById<ProgressBar>(Resource.Id.ytProgress);
|
||||
parseProgress.Visibility = ViewStates.Visible;
|
||||
parseProgress.ScaleY = 6;
|
||||
@@ -1086,78 +1105,28 @@ namespace MusicApp
|
||||
SearchResource.ListRequest searchResult = YoutubeEngine.youtubeService.Search.List("snippet");
|
||||
searchResult.Fields = "items(id/videoId,snippet/title,snippet/thumbnails/default/url,snippet/channelTitle)";
|
||||
searchResult.Type = "video";
|
||||
searchResult.MaxResults = 2;
|
||||
searchResult.MaxResults = 20;
|
||||
searchResult.RelatedToVideoId = MusicPlayer.queue[MusicPlayer.CurrentID()].youtubeID;
|
||||
|
||||
var searchReponse = await searchResult.ExecuteAsync();
|
||||
|
||||
List<Song> result = new List<Song>();
|
||||
List<string> titles = new List<string>
|
||||
{
|
||||
MusicPlayer.queue[MusicPlayer.CurrentID()].GetName()
|
||||
};
|
||||
|
||||
foreach (var video in searchReponse.Items)
|
||||
{
|
||||
Song videoInfo = new Song(video.Snippet.Title, video.Snippet.ChannelTitle, video.Snippet.Thumbnails.Default__.Url, video.Id.VideoId, -1, -1, video.Id.VideoId, true, false);
|
||||
result.Add(videoInfo);
|
||||
titles.Add(video.Snippet.Title);
|
||||
}
|
||||
|
||||
bool useLocalPlay = false;
|
||||
int retryCount = 0;
|
||||
for (int i = 0; i < 26; i += 2)
|
||||
{
|
||||
if (i < 0)
|
||||
{
|
||||
retryCount++;
|
||||
Random random = new Random();
|
||||
i = random.Next(0, result.Count);
|
||||
}
|
||||
|
||||
int errorCount = 0;
|
||||
searchResult.RelatedToVideoId = result[i].youtubeID;
|
||||
searchReponse = await searchResult.ExecuteAsync();
|
||||
foreach (var video in searchReponse.Items)
|
||||
{
|
||||
if (titles.Contains(video.Snippet.Title))
|
||||
{
|
||||
errorCount++;
|
||||
i--;
|
||||
if(errorCount == 2)
|
||||
{
|
||||
retryCount++;
|
||||
Random random = new Random();
|
||||
i = random.Next(0, result.Count - 1);
|
||||
|
||||
if (retryCount > 3)
|
||||
{
|
||||
//Should check parameters and play a commonly played video on yt or play local files
|
||||
useLocalPlay = true;
|
||||
goto End;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
Song videoInfo = new Song(video.Snippet.Title, video.Snippet.ChannelTitle, video.Snippet.Thumbnails.Default__.Url, video.Id.VideoId, -1, -1, video.Id.VideoId, true, false);
|
||||
result.Add(videoInfo);
|
||||
titles.Add(video.Snippet.Title);
|
||||
}
|
||||
}
|
||||
|
||||
End:
|
||||
Random r = new Random();
|
||||
result = result.OrderBy(x => r.Next()).ToList();
|
||||
foreach(Song song in result)
|
||||
Player.instance?.UpdateNext();
|
||||
foreach (Song song in result)
|
||||
{
|
||||
MusicPlayer.instance.AddToQueue(song);
|
||||
await Task.Delay(5);
|
||||
}
|
||||
|
||||
if(useLocalPlay)
|
||||
LocalPlay(null, new EventArgs());
|
||||
|
||||
parseProgress.Visibility = ViewStates.Gone;
|
||||
}
|
||||
|
||||
|
||||
@@ -181,6 +181,9 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
if (adapter == null || adapter.Count == 0)
|
||||
{
|
||||
if (isEmpty)
|
||||
return;
|
||||
|
||||
isEmpty = true;
|
||||
Activity.AddContentView(emptyView, View.LayoutParameters);
|
||||
}
|
||||
|
||||
@@ -204,21 +204,26 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
pictures[0] = new Picture(data);
|
||||
meta.Tag.Pictures = pictures;
|
||||
artURI = null;
|
||||
|
||||
if(!tempFile)
|
||||
artURI = null;
|
||||
|
||||
ContentResolver.Delete(ContentUris.WithAppendedId(Android.Net.Uri.Parse("content://media/external/audio/albumart"), song.GetAlbumArt()), null, null);
|
||||
}
|
||||
|
||||
meta.Save();
|
||||
stream.Dispose();
|
||||
Android.Media.MediaScannerConnection.ScanFile(this, new string[] { song.GetPath() }, null, null);
|
||||
|
||||
if (tempFile)
|
||||
{
|
||||
tempFile = false;
|
||||
System.IO.File.Delete(artURI.ToString());
|
||||
System.IO.File.Delete(artURI.Path);
|
||||
artURI = null;
|
||||
}
|
||||
|
||||
await Task.Delay(10);
|
||||
Android.Media.MediaScannerConnection.ScanFile(this, new string[] { song.GetPath() }, null, null);
|
||||
|
||||
Toast.MakeText(this, "Changes saved.", ToastLength.Short).Show();
|
||||
}
|
||||
|
||||
@@ -251,6 +256,17 @@ namespace MusicApp.Resources.Portable_Class
|
||||
return;
|
||||
}
|
||||
|
||||
const string permission = Manifest.Permission.WriteExternalStorage;
|
||||
if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, permission) != (int)Permission.Granted)
|
||||
{
|
||||
string[] permissions = new string[] { permission };
|
||||
RequestPermissions(permissions, 2659);
|
||||
|
||||
await Task.Delay(1000);
|
||||
while (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, permission) != (int)Permission.Granted)
|
||||
await Task.Delay(500);
|
||||
}
|
||||
|
||||
YoutubeClient client = new YoutubeClient();
|
||||
Video video = await client.GetVideoAsync(youtubeID.Text);
|
||||
title.Text = video.Title;
|
||||
@@ -262,17 +278,19 @@ namespace MusicApp.Resources.Portable_Class
|
||||
System.IO.File.Delete(artURI.ToString());
|
||||
}
|
||||
|
||||
string tempArt = prefManager.GetString("downloadPath", "") + "/albumArt" + Path.GetExtension(video.Thumbnails.HighResUrl);
|
||||
System.Console.WriteLine("&Temp path: " + tempArt);
|
||||
|
||||
WebClient webClient = new WebClient();
|
||||
await webClient.DownloadFileTaskAsync(new System.Uri(video.Thumbnails.HighResUrl), tempArt);
|
||||
webClient.DownloadDataCompleted += (sender, e) =>
|
||||
{
|
||||
string tempArt = Path.Combine(prefManager.GetString("downloadPath", ""), "albumArt" + Path.GetExtension(video.Thumbnails.HighResUrl));
|
||||
System.Console.WriteLine("&Temp path: " + tempArt + "Url: " + video.Thumbnails.HighResUrl);
|
||||
System.IO.File.WriteAllBytes(tempArt, e.Result);
|
||||
|
||||
await Task.Delay(150);
|
||||
Android.Net.Uri uri = Android.Net.Uri.Parse(tempArt);
|
||||
Picasso.With(Application.Context).Load(uri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(albumArt);
|
||||
artURI = uri;
|
||||
tempFile = true;
|
||||
Android.Net.Uri uri = Android.Net.Uri.FromFile(new Java.IO.File(tempArt));
|
||||
Picasso.With(this).Load(uri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(albumArt);
|
||||
artURI = uri;
|
||||
tempFile = true;
|
||||
};
|
||||
webClient.DownloadDataAsync(new System.Uri(video.Thumbnails.HighResUrl));
|
||||
}
|
||||
|
||||
async void DownloadAlbumArtOnYT()
|
||||
@@ -290,8 +308,19 @@ namespace MusicApp.Resources.Portable_Class
|
||||
return;
|
||||
}
|
||||
|
||||
const string permission = Manifest.Permission.WriteExternalStorage;
|
||||
if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, permission) != (int)Permission.Granted)
|
||||
{
|
||||
string[] permissions = new string[] { permission };
|
||||
RequestPermissions(permissions, 2659);
|
||||
|
||||
await Task.Delay(1000);
|
||||
while (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, permission) != (int)Permission.Granted)
|
||||
await Task.Delay(500);
|
||||
}
|
||||
|
||||
YoutubeClient client = new YoutubeClient();
|
||||
Video video = await client.GetVideoAsync(song.youtubeID);
|
||||
Video video = await client.GetVideoAsync(youtubeID.Text);
|
||||
|
||||
if (tempFile)
|
||||
{
|
||||
@@ -299,17 +328,19 @@ namespace MusicApp.Resources.Portable_Class
|
||||
System.IO.File.Delete(artURI.ToString());
|
||||
}
|
||||
|
||||
string tempArt = prefManager.GetString("downloadPath", "") + "/albumArt" + Path.GetExtension(video.Thumbnails.HighResUrl);
|
||||
System.Console.WriteLine("&Temp path: " + tempArt);
|
||||
|
||||
WebClient webClient = new WebClient();
|
||||
await webClient.DownloadFileTaskAsync(new System.Uri(video.Thumbnails.HighResUrl), tempArt);
|
||||
webClient.DownloadDataCompleted += (sender, e) =>
|
||||
{
|
||||
string tempArt = Path.Combine(prefManager.GetString("downloadPath", ""), "albumArt" + Path.GetExtension(video.Thumbnails.HighResUrl));
|
||||
System.Console.WriteLine("&Temp path: " + tempArt + "Url: " + video.Thumbnails.HighResUrl);
|
||||
System.IO.File.WriteAllBytes(tempArt, e.Result);
|
||||
|
||||
await Task.Delay(150);
|
||||
Android.Net.Uri uri = Android.Net.Uri.Parse(tempArt);
|
||||
Picasso.With(Application.Context).Load(uri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(albumArt);
|
||||
artURI = uri;
|
||||
tempFile = true;
|
||||
Android.Net.Uri uri = Android.Net.Uri.FromFile(new Java.IO.File(tempArt));
|
||||
Picasso.With(this).Load(uri).Placeholder(Resource.Drawable.MusicIcon).Resize(400, 400).CenterCrop().Into(albumArt);
|
||||
artURI = uri;
|
||||
tempFile = true;
|
||||
};
|
||||
webClient.DownloadDataAsync(new System.Uri(video.Thumbnails.HighResUrl));
|
||||
}
|
||||
|
||||
void UndoChange()
|
||||
|
||||
@@ -174,18 +174,38 @@ namespace MusicApp.Resources.Portable_Class
|
||||
.SetUsage(AudioUsageKind.Media)
|
||||
.SetContentType(AudioContentType.Music)
|
||||
.Build();
|
||||
AudioFocusRequestClass focusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
|
||||
|
||||
if(Build.VERSION.SdkInt >= BuildVersionCodes.O)
|
||||
{
|
||||
AudioFocusRequestClass focusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
|
||||
.SetAudioAttributes(attributes)
|
||||
.SetAcceptsDelayedFocusGain(true)
|
||||
.SetOnAudioFocusChangeListener(this)
|
||||
.Build();
|
||||
var audioFocus = audioManager.RequestAudioFocus(focusRequest);
|
||||
|
||||
if (audioFocus != AudioFocusRequest.Granted)
|
||||
{
|
||||
Console.WriteLine("Can't Get Audio Focus");
|
||||
return;
|
||||
AudioFocusRequest audioFocus = audioManager.RequestAudioFocus(focusRequest);
|
||||
|
||||
if (audioFocus != AudioFocusRequest.Granted)
|
||||
{
|
||||
Console.WriteLine("Can't Get Audio Focus");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
|
||||
AudioManager am = (AudioManager)MainActivity.instance.GetSystemService(AudioService);
|
||||
|
||||
AudioFocusRequest audioFocus = am.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);
|
||||
|
||||
if (audioFocus != AudioFocusRequest.Granted)
|
||||
{
|
||||
Console.WriteLine("Can't Get Audio Focus");
|
||||
return;
|
||||
}
|
||||
#pragma warning restore CS0618
|
||||
}
|
||||
|
||||
player.PlayWhenReady = true;
|
||||
player.Prepare(mediaSource, true, true);
|
||||
|
||||
@@ -230,17 +250,38 @@ namespace MusicApp.Resources.Portable_Class
|
||||
.SetUsage(AudioUsageKind.Media)
|
||||
.SetContentType(AudioContentType.Music)
|
||||
.Build();
|
||||
AudioFocusRequestClass focusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
|
||||
|
||||
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
|
||||
{
|
||||
AudioFocusRequestClass focusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
|
||||
.SetAudioAttributes(attributes)
|
||||
.SetAcceptsDelayedFocusGain(true)
|
||||
.SetOnAudioFocusChangeListener(this)
|
||||
.Build();
|
||||
var audioFocus = audioManager.RequestAudioFocus(focusRequest);
|
||||
if (audioFocus != AudioFocusRequest.Granted)
|
||||
{
|
||||
Console.WriteLine("Can't Get Audio Focus");
|
||||
return;
|
||||
AudioFocusRequest audioFocus = audioManager.RequestAudioFocus(focusRequest);
|
||||
|
||||
if (audioFocus != AudioFocusRequest.Granted)
|
||||
{
|
||||
Console.WriteLine("Can't Get Audio Focus");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
|
||||
AudioManager am = (AudioManager)MainActivity.instance.GetSystemService(AudioService);
|
||||
|
||||
AudioFocusRequest audioFocus = am.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);
|
||||
|
||||
if (audioFocus != AudioFocusRequest.Granted)
|
||||
{
|
||||
Console.WriteLine("Can't Get Audio Focus");
|
||||
return;
|
||||
}
|
||||
#pragma warning restore CS0618
|
||||
}
|
||||
|
||||
player.PlayWhenReady = true;
|
||||
player.Prepare(mediaSource, true, true);
|
||||
CreateNotification(song.GetName(), song.GetArtist(), song.GetAlbumArt(), song.GetAlbum());
|
||||
@@ -772,4 +813,5 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
//SmallOnTop
|
||||
Preference smallOnTopPreference = PreferenceScreen.FindPreference("smallOnTop");
|
||||
smallOnTopPreference.PreferenceClick += SmallOnTop;
|
||||
smallOnTopPreference.Summary = prefManager.GetBoolean("smallOnTop", false) ? "True" : "False";
|
||||
smallOnTopPreference.Summary = prefManager.GetBoolean("smallOnTop", false) ? "Comming soon" : "Comming Soon"/*"True" : "False"*/;
|
||||
}
|
||||
|
||||
private async void AskForPermission()
|
||||
|
||||
Reference in New Issue
Block a user