/// <summary> /// Play songs from the channel of your choice. /// </summary> /// <param name="ChannelID"></param> public async static void MixFromChannel(string ChannelID) { if (!await MainActivity.instance.WaitForYoutube()) { return; } List <Song> songs = new List <Song>(); try { SearchResource.ListRequest searchRequest = YoutubeService.Search.List("snippet"); searchRequest.Fields = "items(id/videoId,snippet/title,snippet/thumbnails/high/url,snippet/channelTitle)"; searchRequest.Type = "video"; searchRequest.ChannelId = ChannelID; searchRequest.MaxResults = 20; var searchReponse = await searchRequest.ExecuteAsync(); foreach (var video in searchReponse.Items) { Song song = new Song(WebUtility.HtmlDecode(video.Snippet.Title), video.Snippet.ChannelTitle, video.Snippet.Thumbnails.High.Url, video.Id.VideoId, -1, -1, null, true, false) { ChannelID = video.Snippet.ChannelId }; songs.Add(song); } } catch (System.Net.Http.HttpRequestException) { MainActivity.instance.Timout(); } Random r = new Random(); songs = songs.OrderBy(x => r.Next()).ToList(); SongManager.Play(songs[0]); songs.RemoveAt(0); while (MusicPlayer.instance == null) { await Task.Delay(10); } MusicPlayer.instance.AddToQueue(songs); }
/// <summary> /// RandomPlay all file in the folder you selected or all file on the device if the folderPath = null. /// </summary> /// <param name="folderPath">The path of a folder if you want to shuffle files from this folder only.</param> public async static void ShuffleAll(string folderPath = null) { if (!await MainActivity.instance.GetReadPermission()) { return; } List <Song> songs = new List <Song>(); await Task.Run(() => { if (Looper.MyLooper() == null) { Looper.Prepare(); } CursorLoader cursorLoader = new CursorLoader(Application.Context, MediaStore.Audio.Media.ExternalContentUri, null, MediaStore.Audio.Media.InterfaceConsts.Data + " LIKE \"%" + folderPath + "%\"", null, null); ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); if (musicCursor != null && musicCursor.MoveToFirst()) { int titleID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Title); int artistID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Artist); int albumID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Album); int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id); int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data); do { string Artist = musicCursor.GetString(artistID); string Title = musicCursor.GetString(titleID); string Album = musicCursor.GetString(albumID); long AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId)); long id = musicCursor.GetLong(thisID); string path = musicCursor.GetString(pathID); if (Title == null) { Title = "Unknown Title"; } if (Artist == null) { Artist = "Unknow Artist"; } if (Album == null) { Album = "Unknow Album"; } songs.Add(new Song(Title, Artist, Album, null, AlbumArt, id, path)); }while (musicCursor.MoveToNext()); musicCursor.Close(); } }); if (songs.Count == 0) { Snackbar snackBar = Snackbar.Make(MainActivity.instance.FindViewById <CoordinatorLayout>(Resource.Id.snackBar), Resource.String.no_song_mix, Snackbar.LengthLong); snackBar.View.FindViewById <TextView>(Resource.Id.snackbar_text).SetTextColor(Color.White); snackBar.Show(); return; } Random r = new Random(); songs = songs.OrderBy(x => r.Next()).ToList(); SongManager.Play(songs[0]); songs.RemoveAt(0); await Task.Delay(1000); while (MusicPlayer.instance == null) { await Task.Delay(10); } MusicPlayer.instance.AddToQueue(songs); }
/// <summary> /// Add a list of related songs to the queue. /// </summary> /// <param name="item"></param> public static async void CreateMixFromSong(Song item) { bool AddItemToQueue = true; if (MusicPlayer.queue.Count == 0) { SongManager.Play(item); AddItemToQueue = false; } ProgressBar parseProgress = MainActivity.instance.FindViewById <ProgressBar>(Resource.Id.ytProgress); parseProgress.Visibility = ViewStates.Visible; parseProgress.ScaleY = 6; if (!await MainActivity.instance.WaitForYoutube()) { Snackbar snackBar = Snackbar.Make(MainActivity.instance.FindViewById(Resource.Id.snackBar), Resource.String.youtube_loading_error, Snackbar.LengthLong); snackBar.View.FindViewById <TextView>(Resource.Id.snackbar_text).SetTextColor(Color.White); snackBar.Show(); return; } List <Song> tracks = new List <Song>(); try { YoutubeClient client = new YoutubeClient(); var video = await client.GetVideoAsync(item.YoutubeID); var ytPlaylistRequest = YoutubeService.PlaylistItems.List("snippet, contentDetails"); ytPlaylistRequest.PlaylistId = video.GetVideoMixPlaylistId(); ytPlaylistRequest.MaxResults = 25; var ytPlaylist = await ytPlaylistRequest.ExecuteAsync(); foreach (var ytItem in ytPlaylist.Items) { if (ytItem.Snippet.Title != "[Deleted video]" && ytItem.Snippet.Title != "Private video" && ytItem.Snippet.Title != "Deleted video" && !MusicPlayer.queue.Exists(x => x.YoutubeID == ytItem.ContentDetails.VideoId)) { Song song = new Song(WebUtility.HtmlDecode(ytItem.Snippet.Title), ytItem.Snippet.ChannelTitle, ytItem.Snippet.Thumbnails.High.Url, ytItem.ContentDetails.VideoId, -1, -1, ytItem.ContentDetails.VideoId, true, false) { ChannelID = ytItem.Snippet.ChannelId }; tracks.Add(song); } } } catch (Exception ex) { if (ex is System.Net.Http.HttpRequestException) { MainActivity.instance.Timout(); } else { MainActivity.instance.UnknowError(ErrorCode.SM1); } return; } Random r = new Random(); tracks = tracks.OrderBy(x => r.Next()).ToList(); if (AddItemToQueue && !MusicPlayer.queue.Exists(x => x.YoutubeID == item.YoutubeID)) { tracks.Add(item); } Intent intent = new Intent(MainActivity.instance, typeof(MusicPlayer)); MainActivity.instance.StartService(intent); while (MusicPlayer.instance == null) { await Task.Delay(100); } MusicPlayer.instance.AddToQueue(tracks); MainActivity.instance.ShowPlayer(); Home.instance?.RefreshQueue(); Queue.instance?.Refresh(); parseProgress.Visibility = ViewStates.Gone; }
/// <summary> /// Play all file from a folder in order. /// </summary> /// <param name="folderPath">The path of a folder you want to play.</param> /// <param name="startingPosition">The position you want to start playing.</param> /// <param name="customSelection">If you want to have custom selection for your query (can be because the user searched...).</param> public async static void PlayInOrder(string folderPath, int startingPosition = 0, string customSelection = null) { List <Song> tracks = new List <Song>(); await Task.Run(() => { if (Looper.MyLooper() == null) { Looper.Prepare(); } CursorLoader cursorLoader; if (customSelection == null) { cursorLoader = new CursorLoader(Application.Context, MediaStore.Audio.Media.ExternalContentUri, null, MediaStore.Audio.Media.InterfaceConsts.Data + " LIKE \"%" + folderPath + "%\"", null, null); } else { cursorLoader = new CursorLoader(Application.Context, MediaStore.Audio.Media.ExternalContentUri, null, customSelection + " AND " + MediaStore.Audio.Media.InterfaceConsts.Data + " LIKE \"%" + folderPath + "%\"", null, null); } ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); if (musicCursor != null && musicCursor.MoveToFirst()) { int titleID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Title); int artistID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Artist); int albumID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Album); int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id); int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data); do { string Artist = musicCursor.GetString(artistID); string Title = musicCursor.GetString(titleID); string Album = musicCursor.GetString(albumID); long AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId)); long id = musicCursor.GetLong(thisID); string path = musicCursor.GetString(pathID); if (Title == null) { Title = "Unknown Title"; } if (Artist == null) { Artist = "Unknow Artist"; } if (Album == null) { Album = "Unknow Album"; } tracks.Add(new Song(Title, Artist, Album, null, AlbumArt, id, path)); }while (musicCursor.MoveToNext()); musicCursor.Close(); } }); if (tracks.Count == 0) { Snackbar snackBar = Snackbar.Make(MainActivity.instance.FindViewById <CoordinatorLayout>(Resource.Id.snackBar), Resource.String.no_song_mix, Snackbar.LengthLong); snackBar.View.FindViewById <TextView>(Resource.Id.snackbar_text).SetTextColor(Color.White); snackBar.Show(); return; } SongManager.Play(tracks[startingPosition]); tracks.RemoveAt(startingPosition); await Task.Delay(1000); while (MusicPlayer.instance == null) { await Task.Delay(10); } MusicPlayer.instance.InsertToQueue(0, tracks.GetRange(0, startingPosition).ToArray()); MusicPlayer.currentID = startingPosition; Queue.instance?.RefreshCurrent(); Player.instance?.RefreshPlayer(); MusicPlayer.instance.AddToQueue(tracks.GetRange(startingPosition, tracks.Count - startingPosition)); }