如何按播放列表名称Android编程显示音乐名称

时间:2013-12-13 16:04:06

标签: android mediastore playlist

这里已经发布了一些类似的问题,我已经使用了这些建议并以不同的方式尝试了,但仍然没有结果,我的代码是这样的:

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;

但是在这些代码之后,我怎样才能准确获取音乐列表名称?然后存储在投影中,但我的投影变量始终没有音乐列表信息。如果我需要再次迭代这个光标来获取音乐列表名称,我怎么能这样做,我已经尝试过这种方式..但是它不起作用。

// 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);

        }   

看起来像hasItem一样空。我被困在这几个小时,也许这是一个愚蠢的问题,但任何建议都会优雅。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望列出给定播放列表中的曲目。 我就是这样做的。获得光标后,只需获取所需的值即可环绕它。 见下面的例子:

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