kdroider
kdroider

Reputation: 731

How to delete a playlist from MediaStore.Audio.Playlists in Android

If possible, how can I delete a playlist from MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, programatically?

Upvotes: 1

Views: 3332

Answers (3)

Ashish Chaugule
Ashish Chaugule

Reputation: 1593

/**
 * Delete Playlist Track
 *
 * @param context
 * @param playlistId
 * @param audioId
 */
public static void deletePlaylistTrack(Context context, long playlistId, long audioId) {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    String filter = MediaStore.Audio.Playlists.Members.AUDIO_ID + " = " + audioId;
    resolver.delete(uri, filter, null);
}

/**
 * Delete playlist
 *
 * @param context
 * @param selectedplaylist
 */
public static void deletePlaylist(Context context, String selectedplaylist) {
    String playlistid = getPlayListId(context, selectedplaylist);
    ContentResolver resolver = context.getContentResolver();
    String where = MediaStore.Audio.Playlists._ID + "=?";
    String[] whereVal = {playlistid};
    resolver.delete(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, where, whereVal);
}

Upvotes: 0

Theo
Theo

Reputation: 2042

to rename I have the following:

private final Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;

public void renamePlaylist(Context context, String newplaylist, long playlist_id) {
    ContentResolver resolver = context.getContentResolver();
    ContentValues values = new ContentValues();
    String where = MediaStore.Audio.Playlists._ID + " =? ";
    String[] whereVal = { Long.toString(playlist_id) };
    values.put(MediaStore.Audio.Playlists.NAME, newplaylist);
    resolver.update(uri, values, where, whereVal);
}

Upvotes: 0

Theo
Theo

Reputation: 2042

I use the following code to delete a specific playlist. All it needs is the playlist id of course

private void deletePlaylist(String selectedplaylist) 
{
// // Log.i(TAG, "deletePlaylist");
String playlistid = getPlayListId(selectedplaylist);
ContentResolver resolver = this.getContentResolver();
String where = MediaStore.Audio.Playlists._ID + "=?";
String[] whereVal = {playlistid}; 
resolver.delete(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, where, whereVal);
Toast toast = Toast.makeText(this,selectedplaylist + " Deleted", Toast.LENGTH_SHORT);
    toast.show();   
return ;        
}

I have created a small app where you can see this in action. https://play.google.com/store/apps/details?id=rapc.flyingdutchman.com&feature=nav_result#?t=W251bGwsMSwyLDNd

As you can see, it first gets the playlistid. all i have at this point is the playlist name. below my code to get the id.

    public String getPlayListId(String playlist )

    {

    //  read this record and get playlistid

    Uri newuri =MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;

    final String playlistid = MediaStore.Audio.Playlists._ID;

    final String playlistname = MediaStore.Audio.Playlists.NAME;

    String where = MediaStore.Audio.Playlists.NAME + "=?";

    String[] whereVal = {playlist}; 

    String[] projection = {playlistid, playlistname};

    ContentResolver resolver = getContentResolver();

    Cursor record = resolver.query(newuri , projection, where, whereVal, null);

    int recordcount = record.getCount();

    String foundplaylistid = "";

    if (recordcount > 0)

    {
    record.moveToFirst();

    int idColumn = record.getColumnIndex(playlistid);

    foundplaylistid = record.getString(idColumn);

    record.close();
    }

    return foundplaylistid;
    }

Upvotes: 7

Related Questions