How to display music name by playlist name Android programming

1.4k views Asked by At

There are some similar questions have been posted here, I have used those suggestions and tried it in different ways, but still no results for getting out, my codes is like this:

private Cursor getPlaylists(String playlistName) {
    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.NAME + "=\"" + playlistName + "\"", null,
            null); // I need to put "" for the string, otherwise sqlite  errors for no such table
    startManagingCursor(cursor);
    cursor.moveToFirst();
    String playlist_id = cursor.getString(0);
    Long 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 = getContentResolver().query(MediaStore.Audio.Playlists.Members.getContentUri("external",playlist_id2),
                projection,
                null,
                null,
                null);

    }

    return cursor;

But after these codes, how can I exactly get music list names? does then store in projection, but my projection variable are always no music list information inside. If I need to iterate this cursor again to get music list names, how can I do it, I have tried this way.. but it won't work.

// startManagingCursor(cursor);

       for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor
                .moveToNext()) {
            String musicName = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Playlists.Members.TITLE));
            Log.i(LOGGING_TAG, musicName);

        }   

it looks like hasItem is null as always. I am stuck on this for couple of hours, maybe that is a stupid question but any suggestions would be graceful.

1

There are 1 answers

0
Theo On

If I understand you correctly, you want to list the tracks on a given playlist. This is how I do it. Once you have your cursor, you just loop around it by getting the values you need. see examples below :

public Cursor getPlaylistTracks(Context context, Long playlist_id) {
    Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri("external",playlist_id);
    ContentResolver resolver = context.getContentResolver();
    String _id = MediaStore.Audio.Playlists.Members._ID;
    String audio_id = MediaStore.Audio.Playlists.Members.AUDIO_ID;
    String artist = MediaStore.Audio.Playlists.Members.ARTIST;
    String album = MediaStore.Audio.Playlists.Members.ALBUM;
    String title = MediaStore.Audio.Playlists.Members.TITLE;
    String duration = MediaStore.Audio.Playlists.Members.DURATION;
    String location = MediaStore.Audio.Playlists.Members.DATA;
    String composer = MediaStore.Audio.Playlists.Members.COMPOSER;
    String playorder = MediaStore.Audio.Playlists.Members.PLAY_ORDER;
    String date_modified = MediaStore.Audio.Playlists.Members.DATE_MODIFIED;
    String[] columns = { _id, audio_id, artist, album, title, duration,
            location, date_modified, playorder, composer };
    Cursor cursor = resolver.query(newuri, columns, null, null, null);
    return cursor;
}


    if (cursor!= null){
        cursor.moveToFirst();
        int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.ARTIST);
        int durationColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.DURATION);

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
        {
         artist = cursor.getString(artistColumn);
        duration = duration + cursor.getLong(durationColumn);
                    // do something else
        }
        cursor.close();
    }