我正在用Android开发一个媒体播放器应用程序,当在数据库中创建一个播放列表,然后尝试修改它时,比如从播放列表中删除歌曲或将歌曲移动到其他位置(该应用程序具有重新排序功能,拖放),它根本不起作用。我使用这两个代码来删除和重新排序:
public boolean movePlaylistSong(int playlistId, int from, int to){
try{
return MediaStore.Audio.Playlists.Members.moveItem(context.getContentResolver(), playlistId, from, to);
}catch(Exception e){
Logger.e(TAG, e.getMessage());
}
return false;
}
public boolean removeFromPlaylist(int playlistId, int audioId) {
try{
ContentResolver resolver = context.getContentResolver();
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
return resolver.delete(uri, MediaStore.Audio.Playlists.Members.AUDIO_ID +"=?", new String[]{String.valueOf(audioId)}) != 0;
}catch(Exception e){
e.printStackTrace();
}
return false;
}它们都返回true,表示成功,但是当再次从数据库重新加载播放列表(播放列表的外部内容uri )时,它返回原始的播放列表,而没有应用任何更改。它返回成功结果,但实际上并不起作用。
提前谢谢。
发布于 2013-11-15 17:05:12
调用moveItem将更改PLAY_ORDER的值,但您需要按光标上的PLAY_ORDER进行排序才能看到更改。否则,光标将按_ID排序,它不会被编辑。
Uri uriPlaylistTracks = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID);
String sortOrder = MediaStore.Audio.Playlists.Members.PLAY_ORDER;
cursor = resolver.query(uriPlaylistTracks, STAR, null, null, sortOrder);要更改PLAY_ORDER,我执行了以下操作:
//get the PLAY_ORDER values for song
cursor.moveToPosition(fromPosition);
int from = (int) cursor.getLong(cursor.getColumnIndex(Audio.Playlists.Members.PLAY_ORDER));
cursor.moveToPosition(toPosition); //position in list
int to = (int) cursor.getLong(cursor.getColumnIndex(Audio.Playlists.Members.PLAY_ORDER));
//update the PLAY_ORDER values using moveItem
boolean result = MediaStore.Audio.Playlists.Members.moveItem(resolver,
playlistID, from, to);
//get new cursor with the updated PLAY_ORDERs
cursor = resolver.query(uriPlaylistTracks, STAR, null, null, sortOrder);
//change the cursor for a listview adapter
adapter.changeCursor(cursor);
adapter.notifyDataSetChanged();发布于 2013-09-29 18:58:48
正在与同样的问题作斗争。行为为成功,但没有更改。但是,我确实注意到您的playlistid是int类型,但是文档说明很长。
public static final boolean moveItem (ContentResolver res, long playlistId, int from, int to) 我也在论坛Alternative to MediaStore.Playlists.Members.moveItem上找到了这个答案。
我发现它可以移动PLAY_ORDER,但不能移动AUDIO_ID。
https://stackoverflow.com/questions/18168470
复制相似问题