0

データベースにプレイリストを作成し、プレイリストから曲を削除したり、曲を他の位置に移動したりするなど、メディアプレーヤーアプリでAndroidを使用しています(アプリには並べ替え機能、ドラッグアンドドロップがあります) 、それは単に機能しません。削除と並べ替えに次の 2 つのコードを使用しています。

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) からプレイリストを再度リロードすると、変更が適用されていない元のものが返されます。成功の結果を返しますが、実際には機能しませんでした。

前もって感謝します。

4

2 に答える 2

1

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-11-15T09:05:11.607 に答える