Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to MediaStore.Playlists.Members.moveItem

Tags:

android

I've been using the following code to remove an item from a playlist in my Android app:

private void removeFromPlaylist(long playlistId, int loc)
{
    ContentResolver resolver = getApplicationContext().getContentResolver();
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    resolver.delete(uri, MediaStore.Audio.Playlists.Members.PLAY_ORDER+" = "+loc, null);
    for(int i=loc+1;i<playSongIDs.length;i++) {
        MediaStore.Audio.Playlists.Members.moveItem(resolver,playlistId,i, i-1);
    }
}

I'm currently using the Android 2.2 library and this is the only thing that I need to change to use Android 2.1. Is there an alternate method for removing an item from a playlist and adjusting the order of the items after the deleted one?

like image 390
jathak Avatar asked Nov 14 '22 14:11

jathak


1 Answers

looking at the code of the MediaStore we came out with this solution that seems to work fine:

/**
 * Convenience method to move a playlist item to a new location
 * @param res The content resolver to use
 * @param playlistId The numeric id of the playlist
 * @param from The position of the item to move
 * @param to The position to move the item to
 * @return true on success
 */
private boolean moveItem(ContentResolver res, long playlistId, int from, int to) {
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
            playlistId)
            .buildUpon()
            .appendEncodedPath(String.valueOf(from))
            .appendQueryParameter("move", "true")
            .build();
    ContentValues values = new ContentValues();
    values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, to);
    return res.update(uri, values, null, null) != 0;
}
like image 96
Mario Lenci Avatar answered Nov 16 '22 02:11

Mario Lenci