This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LocalManager.cs
350 lines (312 loc) · 15 KB
/
LocalManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using Android.App;
using Android.Database;
using Android.Graphics;
using Android.Net;
using Android.OS;
using Android.Provider;
using Android.Support.Design.Widget;
using Android.Support.V4.Content;
using Android.Widget;
using Opus.Api.Services;
using Opus.DataStructure;
using Opus.Fragments;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using TagLib;
using Intent = Android.Content.Intent;
using Random = System.Random;
namespace Opus.Api
{
public class LocalManager
{
#region Playback
/// <summary>
/// Handle playback of a local file using it's path.
/// </summary>
/// <param name="path"></param>
public static void Play(string path)
{
Intent intent = new Intent(Application.Context, typeof(MusicPlayer));
intent.PutExtra("file", path);
Application.Context.StartService(intent);
MainActivity.instance.ShowPlayer();
MusicPlayer.UpdateQueueDataBase();
}
/// <summary>
/// Add a local item to the queue.
/// </summary>
/// <param name="path"></param>
public static void PlayNext(string path)
{
Intent intent = new Intent(Application.Context, typeof(MusicPlayer));
intent.PutExtra("file", path);
intent.SetAction("PlayNext");
Application.Context.StartService(intent);
}
/// <summary>
/// Add a local item at the end of the queue.
/// </summary>
/// <param name="path"></param>
public static void PlayLast(string path)
{
Intent intent = new Intent(Application.Context, typeof(MusicPlayer));
intent.PutExtra("file", path);
intent.SetAction("PlayLast");
Application.Context.StartService(intent);
}
/// <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>
/// 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));
}
#endregion
#region Metadata
/// <summary>
/// Return a song using only it's path (can be normal path or content:// path). This method read metadata in the device database.
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public async static Task<Song> GetSong(string filePath)
{
string Title = "Unknow";
string Artist = "Unknow";
long AlbumArt = 0;
long id = 0;
string path;
Uri musicUri = MediaStore.Audio.Media.ExternalContentUri;
if (filePath.StartsWith("content://"))
musicUri = Uri.Parse(filePath);
await Task.Run(() =>
{
if (Looper.MyLooper() == null)
Looper.Prepare();
CursorLoader cursorLoader = new CursorLoader(Application.Context, musicUri, null, null, 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 thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id);
int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data);
do
{
path = musicCursor.GetString(pathID);
if (path == filePath || filePath.StartsWith("content://"))
{
Artist = musicCursor.GetString(artistID);
Title = musicCursor.GetString(titleID);
AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId));
id = musicCursor.GetLong(thisID);
if (Title == null)
Title = "Unknown Title";
if (Artist == null)
Artist = "Unknow Artist";
if (filePath.StartsWith("content://"))
filePath = path;
break;
}
}
while (musicCursor.MoveToNext());
musicCursor.Close();
}
});
return new Song(Title, Artist, null, null, AlbumArt, id, filePath);
}
/// <summary>
/// Read the youtubeID of a local song in the file's metadata and add it to the song object.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public static Song CompleteItem(Song item)
{
item.YoutubeID = GetYtID(item.Path);
return item;
}
/// <summary>
/// Read the youtubeID of a local song in the file's metadata using the path of the file.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetYtID(string path)
{
try
{
using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var meta = TagLib.File.Create(new StreamFileAbstraction(path, stream, stream));
return meta.Tag.Comment;
}
}
catch
{
System.Console.WriteLine("&GetYtID Error: The format of the file at " + path + " is probably unsupported.");
return null;
}
}
/// <summary>
/// Open the EditMetadata page for a song.
/// </summary>
/// <param name="item"></param>
/// <param name="queuePosition">Set this to a position of the song in the queue, it will update the queue slot after the edit.</param>
public static void EditMetadata(Song item, int queuePosition = -1)
{
item = CompleteItem(item);
Intent intent = new Intent(Application.Context, typeof(EditMetaData));
intent.PutExtra("Song", item.ToString());
intent.PutExtra("Position", queuePosition);
MainActivity.instance.StartActivity(intent);
}
#endregion
#region PlaylistsImplementation
/// <summary>
/// Return true if the local song of id audioID is contained in the local playlist with the id of playlistID.
/// </summary>
/// <param name="audioID"></param>
/// <param name="playlistID"></param>
/// <returns></returns>
public async static Task<bool> SongIsContained(long audioID, long playlistID)
{
Uri uri = MediaStore.Audio.Playlists.Members.GetContentUri("external", playlistID);
return await Task.Run(() =>
{
if (Looper.MyLooper() == null)
Looper.Prepare();
CursorLoader loader = new CursorLoader(Application.Context, uri, null, null, null, null);
ICursor cursor = (ICursor)loader.LoadInBackground();
if (cursor != null && cursor.MoveToFirst())
{
int idColumn = cursor.GetColumnIndex(MediaStore.Audio.Playlists.Members.AudioId);
do
{
long id = cursor.GetLong(idColumn);
if (id == audioID)
return true;
}
while (cursor.MoveToNext());
cursor.Close();
}
return false;
});
}
#endregion
}
}