2

在 上Android 11,当尝试从 中删除多个曲目时Playlist,所有曲目都将被删除,而不仅仅是选定的曲目。这曾经可以正常工作,直到Android 10.
但是,如果仅删除 1 个轨道,则不会删除其他轨道。这是一个错误还是预期的行为Android 11?这是 的副作用scoped storage吗?
测试:Pixel 3, Android 11 Official release build

 public void removeTracksFromPlaylist(long playlistId, String[] ids) {
        Log.d(TAG, "removeTracksFromPlaylist() called with: playlistId = [" + playlistId + "], ids = [" + Arrays.toString(ids) + "]");
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(MediaStore.VOLUME_EXTERNAL, playlistId);
        StringBuilder stringBuilder = new StringBuilder();
        for (int index = 0; index < ids.length; index++) {
            if (index > 0) {
                stringBuilder.append(",");
            }
            stringBuilder.append("?");
        }
        String selection = MediaStore.Audio.Playlists.Members._ID + " IN (" + stringBuilder.toString() + ")";
        contentResolver.delete(uri, selection, ids); // Returns count of all files removed from playlist
    }

编辑:来自来源的一些信息:(https://android.googlesource.com/platform/packages/providers/MediaProvider/+blame/refs/tags/android-11.0.0_r3/src/com/android/providers/media/MediaProvider .java#5610 )

从源代码中,我看到如果播放列表项的计数超过 1,则整个播放列表都会被清除。

try (Cursor c = qb.query(helper,
                new String[] { Playlists.Members.PLAY_ORDER }, queryArgs, null)) {
            if ((c.getCount() == 1) && c.moveToFirst()) {  // This condition fails if more than 1 item to delete
                return c.getInt(0) - 1;
            } else {
                return -1;
            }
}
 

此处使用索引清除播放列表:( https://android.googlesource.com/platform/packages/providers/MediaProvider/+blame/refs/tags/android-11.0.0_r3/src/com/android/providers/媒体/MediaProvider.java#5566 )

 private int removePlaylistMembers(@NonNull Uri playlistUri, @NonNull Bundle queryArgs)
            throws FallbackException {
        final int index = resolvePlaylistIndex(playlistUri, queryArgs);
        try {
            final File playlistFile = queryForDataFile(playlistUri, null);
            final Playlist playlist = new Playlist();
            playlist.read(playlistFile);
            final int count;
            if (index == -1) {      
                count = playlist.asList().size();
                playlist.clear();  //Clears whole playlist
            } else {
                count = 1;
                playlist.remove(index);
            }
            playlist.write(playlistFile);
            resolvePlaylistMembers(playlistUri);
            return count;
        } catch (IOException e) {
            throw new FallbackException("Failed to update playlist", e,
                    android.os.Build.VERSION_CODES.R);
        }
    }
4

0 回答 0