MediaStore.Playlists.Members.moveItem的替代品

时间:2011-11-24 06:14:17

标签: android

我一直在使用以下代码从Android应用中的播放列表中删除项目:

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。是否有另一种方法可以从播放列表中删除项目并在删除项目后调整项目的顺序?

1 个答案:

答案 0 :(得分:1)

查看MediaStore的代码,我们推出了这个似乎运行正常的解决方案:

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