替代MediaStore.Playlists.Members.moveItemPlaylists、MediaStore、moveItem、Members

2023-09-08 00:33:36 作者:我陪你看细水长流﹏

我一直在使用下面的code,除去我的Andr​​oid应用程序从播放列表中的项目:

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);
    }
}

我目前使用了Android 2.2库,这是我需要改变使用Android 2.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?

推荐答案

看MediaStore的code,我们这个解决方案,似乎很好地工作就出来了:

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;
}