Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Create Playlist

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);
    }
like image 208
shaneburgess Avatar asked Jul 06 '10 01:07

shaneburgess


1 Answers

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);
}
like image 106
Michal Avatar answered Oct 23 '22 10:10

Michal