从播放列表

时间:2016-09-23 05:26:45

标签: android cursor playlist mediastore

我想根据特定的播放列表获取歌曲。我获得所有歌曲的音频ID但没有获得任何其他信息,如标题,艺术家,专辑等。我也试过 this这与我的问题相同 这就是我将歌曲添加到播放列表的方式:

public static void addToPlaylist(ContentResolver resolver, long audioId, int playlistId) {
    String[] cols = new String[] {
            "count(*)"
    };
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    Cursor cur = resolver.query(uri, cols, null, null, null);
    cur.moveToFirst();
    final int base = cur.getInt(0);
    cur.close();
    ContentValues values = new ContentValues();
    values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base +(int) audioId));
    values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioId);
    Uri u =resolver.insert(uri, values);
    Log.d("ItemFragment", "Added Playlist Item: " + u + " for AUDIO_ID " + audioId+" and PLAYLIST_ID = "+playlistId);

}

然后我从特定的播放列表中检索歌曲:

public ArrayList<AudioFile> getSongListFromPlayList() {
    ArrayList<AudioFile> audioFiles = new ArrayList<>();
    String[] proj = {   MediaStore.Audio.Playlists.Members.AUDIO_ID};
    Cursor musicCursor = musicResolver.query(MediaStore.Audio.Playlists.Members.getContentUri("external",id)
            , proj, null, null, null);
    int x = 0;
    Log.d(TAG,"cursor count : "+musicCursor.getCount());
    if ((musicCursor != null) && (musicCursor.moveToFirst())) {
        do {
            long a_id = musicCursor.getLong(musicCursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
            Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String[] projection = new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID};
            String selection = MediaStore.Audio.Media._ID + "=?";
            String[] selectionArgs = new String[]{"" + a_id}; //This is the id you are looking for

            Cursor mediaCursor = musicResolver.query(mediaContentUri, projection, selection, selectionArgs, null);
            Log.d(TAG,"second cursor count : "+mediaCursor.getCount());
            if (mediaCursor.getCount() > 0) {
                mediaCursor.moveToPosition(0);
                String title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                String artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String data = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String duration = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                long id = mediaCursor.getLong(mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID));

                audioFiles.add(new AudioFile(id,title,artist,data,duration,x));
                x++;
            }
        }while (musicCursor.moveToNext());
    }
    Log.d(TAG,"size : "+audioFiles.size());
    return audioFiles;
}

在调试时我得到日志:

  

D / SecondActivity:光标数:11
  D / SecondActivity:第二个光标数:1
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:第二个光标数:0
  D / SecondActivity:大小:1

请帮忙。

1 个答案:

答案 0 :(得分:0)

您需要枚举光标并向前移动直到它结束。一旦你有了musicCursor,就可以使用类似的东西来循环播放

             if(musicCursor!= null && musicCursor.moveToFirst()){
            for (musicCursor.moveToFirst(); !musicCursor.isAfterLast(); musicCursor.moveToNext()) {
      // do your stuff here, as you cycle through your musicCursor you will read the next audio_id which then gets the relevant data
            }
       }

for循环将枚举游标。移除moveto位置, 把uri带到循环之外。如果你在这个类中有几个方法,那么把它放到顶部,这样它就可以用于所有方法,但你只定义了一次。定义一次只需要在for循环之外进行一次定义。 我已经编辑了一些代码

  public ArrayList<AudioFile> getSongListFromPlayList() {
ArrayList<AudioFile> audioFiles = new ArrayList<>();
String[] proj = {   MediaStore.Audio.Playlists.Members.AUDIO_ID};
Cursor musicCursor = musicResolver.query(MediaStore.Audio.Playlists.Members.getContentUri("external",id)
        , proj, null, null, null);

        String[] projection = new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID};
        String selection = MediaStore.Audio.Media._ID + "=?";
if ((musicCursor != null) && (musicCursor.moveToFirst())) {
        for (musicCursor.moveToFirst(); !musicCursor.isAfterLast(); musicCursor.moveToNext()) {
        long a_id = musicCursor.getLong(musicCursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));

        String[] selectionArgs = new String[]{"" + a_id}; //This is the id you are looking for
        Cursor mediaCursor = musicResolver.query(mediaContentUri, projection, selection, selectionArgs, null);
            String title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
            String artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String data = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            String duration = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
            long id = mediaCursor.getLong(mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID));
            audioFiles.add(new AudioFile(id,title,artist,data,duration,x));

    }
        }


Log.d(TAG,"size : "+audioFiles.size());
return audioFiles;