Anyone know how to add playlists to Android in code?
I kind of get that I have to insert it into the content resolver but do I have to just put the song id in or do I have to put all of the song's data in?
I have been looking for sample code but haven't found any yet.
EDIT: Found an answer here is how I do it:
public static void addToPlaylist(ContentResolver resolver, int audioId) {
String[] cols = new String[] {
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
final int base = cur.getInt(0);
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + audioId));
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioId);
resolver.insert(uri, values);
}
public static void removeFromPlaylist(ContentResolver resolver, int audioId) {
Log.v("made it to add",""+audioId);
String[] cols = new String[] {
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
final int base = cur.getInt(0);
cur.close();
ContentValues values = new ContentValues();
resolver.delete(uri, MediaStore.Audio.Playlists.Members.AUDIO_ID +" = "+audioId, null);
}
This is corrected code which remove song from Playlist:
public static void removeFromPlaylist(ContentResolver resolver, int audioId)
{
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
resolver.delete(uri, MediaStore.Audio.Playlists.Members._ID +" = "+audioId, null);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With