Given an Android music playlist name, how can one find the songs in the playlist?

后端 未结 3 1859
无人共我
无人共我 2020-12-01 17:41

The playlist names can be found by a query on MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI and then look at the MediaStore.Audio.PlaylistsColumns.NAME<

3条回答
  •  -上瘾入骨i
    2020-12-01 18:06

    Here is a working way to get tracks from a play list. Basically it loops the cursor through all the of the playlist query, and each time it gets the id of a member (track) and using that id of the track we can get other data such as path, artist, duration, album etc.

     ContentResolver contentResolver = getContentResolver();
        Uri playListUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID); //playlistID is the _ID of the given playlist
        MediaMetadataRetriever mr = new MediaMetadataRetriever();
        Cursor cursor = contentResolver.query(playListUri, null, null, null, null);
        if(cursor != null)
        {
            if(cursor.moveToNext()) {
                do {
                    String track_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
                    Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    String[] trackProjection = new String[]{MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.DATA};
                    String selection = MediaStore.Audio.Media._ID + "=?";
                    String[] selectionArgs = new String[]{"" + track_id};
                    Cursor mediaCursor = contentResolver.query(mediaContentUri, trackProjection, selection, selectionArgs, null);
                    if (mediaCursor != null) {
                        if (mediaCursor.getCount() >= 0) {
                            mediaCursor.moveToPosition(0);
                            String song_title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                            String song_artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                            String song_album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                            String song_path = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                        }
                    }
                } while (cursor.moveToNext());
            }
    
        }
    

提交回复
热议问题