Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 column. There is a data column too, MediaStore.Audio.PlaylistsColumns._DATA, but it is returning null.

The list of songs (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI/id) do not seem to show any playlist affiliation.

like image 745
hexatron Avatar asked Apr 22 '10 22:04

hexatron


People also ask

Why can't I see the songs in my playlist on Spotify?

If you're on the free, ad-supported plan, play individual songs on-demand on mobile devices. That's why you can't preview a list of all the tracks contained within an album or a playlist that's not "Pick and Play". However, you should still be able to listen to the shuffled tracks.


2 Answers

Here is a bit from my program:

The gist is that you need the id for the playlist to grab the songs.

Basically you can take my code and change the where statement to .NAME +" = '"+name+"'",

private Cursor getPlaylists(String playlistId){
         Cursor cursor = null;

 String[] projection1 = {
        MediaStore.Audio.Playlists._ID,
        MediaStore.Audio.Playlists.NAME
     };

     cursor = this.managedQuery(
        MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
        projection1,
        MediaStore.Audio.Playlists._ID+ " = "+playlistId+"",
        null,
        null);
startManagingCursor(cursor);
cursor.moveToFirst();
   playlist_id = cursor.getString(0);
   playlist_id2 = cursor.getLong(0);

           if(playlist_id2 > 0){
           String[] projection = {
                   MediaStore.Audio.Playlists.Members.AUDIO_ID,
                   MediaStore.Audio.Playlists.Members.ARTIST,
                   MediaStore.Audio.Playlists.Members.TITLE,
                    MediaStore.Audio.Playlists.Members._ID



                };
           cursor = null;
                cursor = this.managedQuery(
                    MediaStore.Audio.Playlists.Members.getContentUri("external",playlist_id2 ),
                    projection,
                    MediaStore.Audio.Media.IS_MUSIC +" != 0 ",
                    null,
                    null);

           }
           cManager(cursor,2,1);
           return cursor;
       }
like image 163
shaneburgess Avatar answered Oct 15 '22 15:10

shaneburgess


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

    }
like image 26
Tarek Zoabi Avatar answered Oct 15 '22 16:10

Tarek Zoabi