从播放列表中删除歌曲

时间:2014-05-31 12:38:30

标签: android playlist

我正在尝试从播放列表中删除一首歌。但不知道为什么不删除。这些是我在调试模式下检查的值: URI - 内容://媒体/外部/音频/播放列表/ 4599 /成员
WHERE --audio_id =?
selectionArgs [] - [1214]

我在调试时发现控件在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
    }

在我的代码下面发布删除歌曲:

    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

更新 这就是我调用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));

}

任何人都可以建议可能出现的问题吗?

1 个答案:

答案 0 :(得分:6)

这是正确的代码。希望它可以帮助某人:)

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;

 }