未找到Android播放列表内容

时间:2014-08-01 22:56:29

标签: java android android-contentresolver mediastore

public static ArrayList<Song> getPlaylistcontent(Context context,
        long playlistID) {
    ArrayList<Song> songs = new ArrayList<Song>();

    final String idString = MediaStore.Audio.Playlists.Members._ID;
    final String dataString = MediaStore.Audio.Playlists.Members.DATA;

    String[] projection = new String[] { idString, dataString };
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
            playlistID);

    Cursor cursor = context.getContentResolver().query(uri, projection,
            null, null, null);

    if (cursor != null && cursor.moveToFirst()) {

        do {

            final String id = cursor.getString(cursor
                    .getColumnIndex(idString));
            final String path = cursor.getString(cursor
                    .getColumnIndex(dataString));

            Song song = getSong(context, path);
            if (song != null) {
                songs.add(song);
                Log.d("MyTAG", song.getName());
            }

        } while (cursor.moveToNext());

    }

    return songs;
}

public static ArrayList<Playlist> getAllPlaylists(Context context) {
    return helper_getPlaylists(context, null);
}

private static ArrayList<Playlist> helper_getPlaylists(Context context,
        String selection) {

    ArrayList<Playlist> playlists = new ArrayList<Playlist>();

    final String idString = MediaStore.Audio.Playlists._ID;
    final String nameString = MediaStore.Audio.Playlists.NAME;

    Uri externalUri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    String projection[] = new String[] { idString, nameString };

    // sort by date added
    String sortOrder = MediaStore.Audio.Playlists.DATE_ADDED;

    Cursor audioCursor = context.getContentResolver().query(externalUri,
            projection, selection, null, sortOrder);

    if (audioCursor != null && audioCursor.moveToFirst()) {

        do {
            final String id = audioCursor.getString(audioCursor
                    .getColumnIndex(idString));
            final String name = audioCursor.getString(audioCursor
                    .getColumnIndex(nameString));

            Playlist playlist = new Playlist();
            playlist.setName(name);
            playlist.setID(id);
            playlists.add(playlist);

        } while (audioCursor.moveToNext());

    }

    return playlists;
}

这些是我用来获取播放列表内容的2(3)种方法。好吧,他们应该返回播放列表内容,但它没有。方法getSong() 100%工作,不是问题。我在我的程序的另一部分使用它几千次,它很快并且还在代码审查中进行了审查。

我在主要活动中调用getAllPlaylists(),在ListView中显示这些播放列表,当我点击一个项目时,我将开始一个新活动,该活动应显示该播放列表的项目。因此,我通过意图传递点击的播放列表ID,并在新活动中使用它(工作,我确实得到了正确的ID)。

//MainActivity
Arraylist<Playlist> playlists = MediaManager.getAllPlaylists( this );

//New Activity
long playlistID = getIntent().getExtras().getLong("playlist_id"); //works. 

//in an asynctask in new activity
Arraylist<Song> playlistContent = MediaManager.getPlaylistContent( this, playlistID ); //is empty

这让我抓狂!我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果我是正确的,获取播放列表详细信息的意图是有效的,你得到_ids但你没有得到歌曲。请记住,_id是每个播放列表项的行的id,而不是audio_id。我挣扎了一段时间。也许这是你的问题。 这就是我解决它的方法:

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 album_id = MediaStore.Audio.Playlists.Members.ALBUM_ID;
    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_id,album, title, duration,
            location, date_modified, playorder, composer };
    Cursor cursor = resolver.query(newuri, columns, null, null, null);
    return cursor;
}