Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting songs from playlist

I am trying to delete a song from playlist . But don't know why it is not deleting . These are the values I am checking in the debug mode: URI -- content://media/external/audio/playlists/4599/members
WHERE --audio_id=?
selectionArgs[]-- [1214]

I found while debugging that the control skips these lines in ContentResolver.class .

public final int delete(Uri url, String where, String[] selectionArgs)
{

    try {
        long startTime = SystemClock.uptimeMillis(); //skipped
        int rowsDeleted = provider.delete(mPackageName, url, where, selectionArgs);//skipped
        long durationMillis = SystemClock.uptimeMillis() - startTime;//skipped
        maybeLogUpdateToEventLog(durationMillis, url, "delete", where);
        return rowsDeleted;  // returns 0
    }

Posting below my code to delete song:

    public int deletePlaylistTracks(Context context, long playlistId,
        long audioId) {
    ContentResolver resolver = context.getContentResolver();
    int countDel = 0;
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
            playlistId);
            String selection = MediaStore.Audio.Playlists.Members.AUDIO_ID
                + "=?";
        String selectionargs[] = { String.valueOf(audioId) };

        if (resolver.delete(uri, selection, selectionargs) != 0) {

        countDel++;
    }

    Log.d("TAG", "tracks deleted=" + countDel);
    return countDel;}  //returns 0

UPDATE: This is how I am calling deletePlaylistTracks

public void delete(final List<Long> songsDelete, //audioId
        final List<Integer> position)
{
 if (playlistId != -1) {
 for (int i = 0; i < songsDelete.size(); i++) {
 Log.d("song going to del", ""+ songsDelete.get(i)); //audio Id 1214
 int countDel =   utility.deletePlaylistTracks(PlaylistTracks.this,playlistId,songsDelete.get(i));

}

Can anyone please suggest what could be wrong?

like image 423
Siju Avatar asked Sep 04 '25 01:09

Siju


1 Answers

This is the correct code.Hope it helps someone :)

public int deletePlaylistTracks(Context context, long playlistId,
        long audioId) {
    ContentResolver resolver = context.getContentResolver();
    int countDel = 0;
    try {
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(
                "external", playlistId);
        String where = MediaStore.Audio.Playlists.Members._ID + "=?" ; // my mistake was I used .AUDIO_ID here

        String audioId1 = Long.toString(audioId);
        String[] whereVal = { audioId1 };
        countDel=resolver.delete(uri, where,whereVal);      
        Log.d("TAG", "tracks deleted=" + countDel);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return countDel;

 }
like image 167
Siju Avatar answered Sep 06 '25 17:09

Siju