diff --git a/MusicApp/MainActivity.cs b/MusicApp/MainActivity.cs index 9067417..a1b749e 100644 --- a/MusicApp/MainActivity.cs +++ b/MusicApp/MainActivity.cs @@ -237,17 +237,22 @@ namespace MusicApp tabs.SetScrollPosition(selectedTab, 0f, true); } - //AFTER USING FOLDERTRACKS, BROWSE AND FOLDERBROWSE OONCLICK DOES NOT WORK; - public void HideTabs() { TabLayout tabs = FindViewById(Resource.Id.tabs); tabs.RemoveAllTabs(); tabs.Visibility = ViewStates.Gone; ViewPager pager = FindViewById(Resource.Id.pager); - if (pager.Adapter != null) - pager.Adapter.Dispose(); - pager.Adapter = null; + + ViewPagerAdapter adapter = (ViewPagerAdapter) pager.Adapter; + if (adapter != null) + { + for(int i = 0; i < adapter.Count; i++) + SupportFragmentManager.BeginTransaction().Remove(adapter.GetItem(i)).Commit(); + + adapter.Dispose(); + pager.Adapter = null; + } FrameLayout frame = FindViewById(Resource.Id.contentView); frame.Visibility = ViewStates.Visible; diff --git a/MusicApp/MusicApp.csproj b/MusicApp/MusicApp.csproj index f1a308d..d696ce4 100644 --- a/MusicApp/MusicApp.csproj +++ b/MusicApp/MusicApp.csproj @@ -159,6 +159,7 @@ + diff --git a/MusicApp/Resources/Portable Class/Browse.cs b/MusicApp/Resources/Portable Class/Browse.cs index 43973d2..e88d562 100644 --- a/MusicApp/Resources/Portable Class/Browse.cs +++ b/MusicApp/Resources/Portable Class/Browse.cs @@ -11,9 +11,6 @@ using Android.Views; using Android.Content.PM; using Android.Support.V4.App; using Android.Support.V7.App; -using Android.Runtime; -using System; -using Java.Lang; namespace MusicApp.Resources.Portable_Class { @@ -327,40 +324,4 @@ namespace MusicApp.Resources.Portable_Class AddToPlaylist(item, playList, playlistID); } } - - public class ViewPagerAdapter : FragmentPagerAdapter - { - private List fragmentList = new List(); - private List titles = new List(); - - - public ViewPagerAdapter(FragmentManager fm) : base(fm) - { - - } - - protected ViewPagerAdapter(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) - { - - } - - public override int Count => fragmentList.Count; - - public override Fragment GetItem(int position) - { - return fragmentList[position]; - } - - public void AddFragment (Fragment fragment, string title) - { - fragmentList.Add(fragment); - titles.Add(title); - } - - public override ICharSequence GetPageTitleFormatted(int position) - { - ICharSequence title = CharSequence.ArrayFromStringArray(new string[] { titles[position] })[0]; - return title; - } - } } \ No newline at end of file diff --git a/MusicApp/Resources/Portable Class/FolderBrowse.cs b/MusicApp/Resources/Portable Class/FolderBrowse.cs index cd73d2c..eb1fff2 100644 --- a/MusicApp/Resources/Portable Class/FolderBrowse.cs +++ b/MusicApp/Resources/Portable Class/FolderBrowse.cs @@ -139,7 +139,7 @@ namespace MusicApp.Resources.Portable_Class GetPlaylist(path); break; case 2: - //random play + RandomPlay(path); break; default: break; @@ -154,9 +154,10 @@ namespace MusicApp.Resources.Portable_Class act.SupportActionBar.SetHomeButtonEnabled(true); act.SupportActionBar.SetDisplayHomeAsUpEnabled(true); act.SupportActionBar.Title = displayPath; - //MainActivity.instance.HideTabs(); + + MainActivity.instance.HideTabs(); FragmentTransaction transaction = FragmentManager.BeginTransaction(); - transaction.Replace(Resource.Id.pager, FolderTracks.NewInstance(path)); + transaction.Replace(Resource.Id.contentView, FolderTracks.NewInstance(path)); transaction.AddToBackStack(null); transaction.Commit(); } @@ -274,5 +275,35 @@ namespace MusicApp.Resources.Portable_Class AddToPlaylist(path, "foo", playlistID); } + + void RandomPlay(string folderPath) + { + List trackPaths = new List(); + Uri musicUri = MediaStore.Audio.Media.ExternalContentUri; + + CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); + ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); + + + if (musicCursor != null && musicCursor.MoveToFirst()) + { + int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data); + do + { + string path = musicCursor.GetString(pathID); + + if (path.Contains(folderPath)) + trackPaths.Add(path); + } + while (musicCursor.MoveToNext()); + musicCursor.Close(); + } + + + Intent intent = new Intent(Android.App.Application.Context, typeof(MusicPlayer)); + intent.PutStringArrayListExtra("files", trackPaths.ToArray()); + intent.SetAction("RandomPlay"); + Activity.StartService(intent); + } } } \ No newline at end of file diff --git a/MusicApp/Resources/Portable Class/FolderTracks.cs b/MusicApp/Resources/Portable Class/FolderTracks.cs index 7abdb95..daa3799 100644 --- a/MusicApp/Resources/Portable Class/FolderTracks.cs +++ b/MusicApp/Resources/Portable Class/FolderTracks.cs @@ -33,7 +33,6 @@ namespace MusicApp.Resources.Portable_Class base.OnActivityCreated(savedInstanceState); emptyView = LayoutInflater.Inflate(Resource.Layout.NoPlaylist, null); ListView.EmptyView = emptyView; - ListAdapter = adapter; PopulateList(); MainActivity.instance.DisplaySearch(1); @@ -56,14 +55,12 @@ namespace MusicApp.Resources.Portable_Class { instance = new FolderTracks { Arguments = new Bundle() }; instance.path = path; - System.Console.WriteLine("Path : " + path); return instance; } void PopulateList() { Uri musicUri = MediaStore.Audio.Media.GetContentUriForPath(path); - System.Console.WriteLine(path); CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); diff --git a/MusicApp/Resources/Portable Class/MusicPlayer.cs b/MusicApp/Resources/Portable Class/MusicPlayer.cs index 4e33e63..5ddeaea 100644 --- a/MusicApp/Resources/Portable Class/MusicPlayer.cs +++ b/MusicApp/Resources/Portable Class/MusicPlayer.cs @@ -62,9 +62,7 @@ namespace MusicApp.Resources.Portable_Class case "RandomPlay": List files = intent.GetStringArrayListExtra("files").ToList(); -#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed RandomPlay(files); -#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed break; case "PlayNext": @@ -118,6 +116,7 @@ namespace MusicApp.Resources.Portable_Class public async void Play(string filePath) { + Console.WriteLine("Playing : " + filePath); if (player == null) InitializeService(); @@ -159,20 +158,19 @@ namespace MusicApp.Resources.Portable_Class } } - public async Task RandomPlay(List filePath) + public async void RandomPlay(List filePath) { - await Task.Run(async () => + Random r = new Random(); + filePath = filePath.OrderBy(x => r.Next()).ToList(); + await Task.Delay(1000); + Play(filePath[0]); + await Task.Delay(1000); + for (int i = 1; i < filePath.Count; i++) { - Random r = new Random(); - filePath = filePath.OrderBy(x => r.Next()).ToList(); - Play(filePath[0]); - await Task.Delay(1000); - for (int i = 1; i < filePath.Count; i++) - { - GetTrackSong(filePath[i], out Song song); - queue.Add(song); - } - }); + GetTrackSong(filePath[i], out Song song); + queue.Add(song); + await Task.Delay(10); + } } public void AddToQueue(string filePath) @@ -242,7 +240,7 @@ namespace MusicApp.Resources.Portable_Class string path = filePath; Android.Net.Uri musicUri = MediaStore.Audio.Media.ExternalContentUri; - CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); + CursorLoader cursorLoader = new CursorLoader(Application.Context, musicUri, null, null, null, null); ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); if (musicCursor != null && musicCursor.MoveToFirst()) @@ -253,7 +251,9 @@ namespace MusicApp.Resources.Portable_Class int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id); int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data); - while (musicCursor.GetString(pathID) != filePath) + path = musicCursor.GetString(pathID); + + while (path != filePath) musicCursor.MoveToNext(); Artist = musicCursor.GetString(artistID); @@ -261,7 +261,6 @@ namespace MusicApp.Resources.Portable_Class Album = musicCursor.GetString(albumID); AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId)); id = musicCursor.GetLong(thisID); - path = musicCursor.GetString(pathID); if (Title == null) Title = "Unknown Title"; diff --git a/MusicApp/Resources/Portable Class/ViewPagerAdapter.cs b/MusicApp/Resources/Portable Class/ViewPagerAdapter.cs new file mode 100644 index 0000000..84af81e --- /dev/null +++ b/MusicApp/Resources/Portable Class/ViewPagerAdapter.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using Android.Runtime; +using System; +using Java.Lang; +using Android.Support.V4.App; + +namespace MusicApp.Resources.Portable_Class +{ + public class ViewPagerAdapter : FragmentPagerAdapter + { + private List fragmentList = new List(); + private List titles = new List(); + + + public ViewPagerAdapter(FragmentManager fm) : base(fm) + { + + } + + protected ViewPagerAdapter(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) + { + + } + + public override int Count => fragmentList.Count; + + public override Fragment GetItem(int position) + { + return fragmentList[position]; + } + + public void AddFragment (Fragment fragment, string title) + { + fragmentList.Add(fragment); + titles.Add(title); + } + + public override ICharSequence GetPageTitleFormatted(int position) + { + ICharSequence title = CharSequence.ArrayFromStringArray(new string[] { titles[position] })[0]; + return title; + } + } +} \ No newline at end of file