如何从Android中的MediaStore.Audio.Playlists中删除播放列表

时间:2012-10-31 09:20:28

标签: android playlist mediastore

如果可能,如何以编程方式从MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI删除播放列表?

3 个答案:

答案 0 :(得分:6)

我使用以下代码删除特定的播放列表。它所需要的只是当然的播放列表ID

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 ;        
}

我创建了一个小应用,您可以在其中看到这一点。 https://play.google.com/store/apps/details?id=rapc.flyingdutchman.com&feature=nav_result#?t=W251bGwsMSwyLDNd

正如你所看到的,它首先得到了playlistid。此时我所拥有的只是播放列表名称。 在我的代码下方获取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;
    }

答案 1 :(得分:0)

重命名我有以下内容:

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);
}

答案 2 :(得分:0)

/**
 * 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);
}