mirror of
https://github.com/zoriya/Opus.git
synced 2025-12-06 06:26:15 +00:00
Random play
This commit is contained in:
@@ -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<TabLayout>(Resource.Id.tabs);
|
||||
tabs.RemoveAllTabs();
|
||||
tabs.Visibility = ViewStates.Gone;
|
||||
ViewPager pager = FindViewById<ViewPager>(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<FrameLayout>(Resource.Id.contentView);
|
||||
frame.Visibility = ViewStates.Visible;
|
||||
|
||||
@@ -159,6 +159,7 @@
|
||||
<Compile Include="Resources\Portable Class\Preferences.cs" />
|
||||
<Compile Include="Resources\Portable Class\Queue.cs" />
|
||||
<Compile Include="Resources\Portable Class\TwoLineAdapter.cs" />
|
||||
<Compile Include="Resources\Portable Class\ViewPagerAdapter.cs" />
|
||||
<Compile Include="Resources\Portable Class\YTitemToSong.cs" />
|
||||
<Compile Include="Resources\Resource.Designer.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.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<Fragment> fragmentList = new List<Fragment>();
|
||||
private List<string> titles = new List<string>();
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<string> trackPaths = new List<string>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -62,9 +62,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
|
||||
case "RandomPlay":
|
||||
List<string> 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<string> filePath)
|
||||
public async void RandomPlay(List<string> 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";
|
||||
|
||||
44
MusicApp/Resources/Portable Class/ViewPagerAdapter.cs
Normal file
44
MusicApp/Resources/Portable Class/ViewPagerAdapter.cs
Normal file
@@ -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<Fragment> fragmentList = new List<Fragment>();
|
||||
private List<string> titles = new List<string>();
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user