0

In my Music-Player, a Playlist may contain two identical songs (the user adds the same song two times). Let's say the song which is 2 times in the Playlist has the id 7664. When removing this song from the Playlist, ContentResolver.delete(URI_OF_PLAYLIST, "audio_id=7664", null); removes both matches. So how do I only delete one match?

Thanks in advance!

the_dani
  • 2,466
  • 2
  • 20
  • 46
  • I can't test it myself, but I think that [rowid](http://stackoverflow.com/questions/15570096/sqlite-get-rowid) (or [this](http://stackoverflow.com/questions/6500445/rowid-in-sqlite3-and-android)) as a selection criteria could be helpful. – SME_Dev Jan 09 '17 at 16:14

2 Answers2

0

This is how I do it in my app New Playlist Manager:

step 1: create an array with audio Ids

step 2: whilst looping around this, use the .lastindex() function to determine if it exists elsewhere. (otherwise the value should be the same as position)

step 3: if found, remove from audio Ids array by setting it to null (you do not want to change its size) and remove from playlist

step 4: reorder the playorder

      public void dedupe_playlist(Context context, long playlist_id) {
    Cursor cursor = plist.getPlaylistTracks(context, playlist_id);
    ArrayList<String> audio_ids = new ArrayList<>();

    // build up the array with audio_id's
    int i = 0;
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        String audio_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
        audio_ids.add(audio_id);

    }

    i = 0;
    int duplicates = i;
    boolean success = false;
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        String audio_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
        duplicates = audio_ids.lastIndexOf(audio_id);
        if (duplicates > i) {
            success = true;
            cursor.moveToPosition(duplicates);
            int _id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members._ID));
            removePlaylistTrack(context, playlist_id, _id);
            audio_ids.set(duplicates, null);
            cursor.moveToPosition(i - 1);        // move back to test again
        } else {
            i++;
        }
    }

    if (success) {
        renumber_playorder(context, playlist_id);
    }
    try {
        cursor.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}


     public void removePlaylistTrack(Context context, long selectedplaylist,
                                int _id) {

    ContentResolver resolver = context.getContentResolver();
    Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri(
            "external", selectedplaylist);
    try {
        resolver.delete(newuri, MediaStore.Audio.Playlists.Members._ID + " = " + _id, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Theo
  • 2,012
  • 1
  • 16
  • 29
  • Thanks, but I don't understand why the contentresolver shouldn't remove both songs now? The id's are identical and in the same table so it will delete both songs again... – the_dani Jan 09 '17 at 17:31
  • Can you post how you reorder the PlayOrder? And is the play order (if reordered) the index of the row + 1? – the_dani Jan 10 '17 at 21:31
  • In response to your first comment, remember I am removing the int _id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members._ID)), which is different to the audio_id – Theo Jan 11 '17 at 16:48
  • Okey, thank you very much, I forgot that in Playlist.Members AUDIO_ID !=_ID! – the_dani Jan 11 '17 at 16:50
0

To reorder a playlist:

      public void renumber_playorder(Context context, long playlist_id) {
    Cursor cursor = getPlaylistTracks(context, playlist_id);
    Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri(
            "external", playlist_id);
    ContentResolver resolver = context.getContentResolver();
    ContentValues values = new ContentValues();
    int i = 0;
    if (cursor != null && cursor.moveToFirst()) {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members._ID));
            String[] selection = {_id};
            values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i);
            resolver.update(newuri, values, MediaStore.Audio.Playlists.Members._ID + " =? ", selection);
            i++;
        }
        cursor.close();
    }
}
Theo
  • 2,012
  • 1
  • 16
  • 29