Android Media.Audio.Playlists始终不返回播放列表

时间:2016-10-17 08:09:24

标签: android android-mediaplayer playlist

我想检索Android设备中的所有歌曲播放列表。我在网上搜索了代码示例,但查询playlistResolver.query(播放列表,null,null,null,null)总是给我大小0.打印出播放列表名称的do循环永远不会被执行。我在网上尝试了一些解决方案,但都没有成功。感谢任何帮助。谢谢!

 public void getSongList() {
    //retrieve song info
    ContentResolver musicResolver = getContentResolver();
    //retrieve playlists
    ContentResolver playlistResolver = getApplicationContext().getContentResolver();

    Uri playlists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;

    Cursor c = playlistResolver.query(playlists, null, null, null, null);
    long playlistId = 0;
    if(c!=null && c.moveToFirst()) {
        do {
            String plName = c.getString(c.getColumnIndex(MediaStore.Audio.Playlists.NAME));
            playlistId = c.getLong(c.getColumnIndex(MediaStore.Audio.Playlists._ID));
            Log.d("PLAYLIST NAME", "PLAYLIST NAME " + plName);
        } while (c.moveToNext());
    }
    c.close();

    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    String isMusic = MediaStore.Audio.Media.IS_MUSIC + "!= 0";//only get music tracks
    Cursor musicCursor = musicResolver.query(musicUri, null, isMusic, null, null);
    if(musicCursor!=null){
        musicCursor.moveToFirst();
        //get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);
        int durationColumn = musicCursor.getColumnIndex
                (MediaStore.Audio.AudioColumns.DURATION);

        //add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            int thisDuration = musicCursor.getInt(durationColumn);
            //String thisPlaylist = musicCursor.getString(playlistColumn);
            //Log.d("SONG IN PLAYLIST", "SONG " + thisTitle + " IN PLAYLIST " + thisPlaylist);
            songList.add(new Song(thisId, thisTitle, thisArtist, thisDuration));
        }
        while (musicCursor.moveToNext());
    }
}

1 个答案:

答案 0 :(得分:0)

我今天偶然发现了同样的问题。播放列表的全局存储通常为电话\内部存储\播放列表。问题中引用的代码只能从该全局存储中检索播放列表。如果音乐应用在其本地存储中创建了播放列表,则您无法访问这些播放列表。

我安装了两个不同的音乐应用X和Y.

  • 两者都可以创建播放列表,但播放列表都不会进入 默认存储位置
  • 每个应用都会显示自己创建的播放列表。没有任何应用程序 显示其他应用创建的播放列表。

然后我安装了第三个应用程序Z.它在全局存储中创建了播放列表,我可以使用问题中的代码检索它。其中一个应用程序能够显示由Z打包的播放列表,但它继续在其本地存储中创建新的播放列表。