3

大图:GUI 向用户显示他们的播放列表列表。用户选择一个。程序将选择的播放列表传递给下一个显示该播放列表中歌曲的活动。

问题:我可以显示播放列表并注册用户选择,但我似乎无法显示该播放列表的歌曲。

是的,我看到了以下问题:

如何在 Android SDK 上查询播放列表中的歌曲?

给定一个 Android 音乐播放列表名称,如何在播放列表中找到歌曲?

MediaStore.Audio.Playlists.Members.getContentUri 的 String 'volumeName' 参数指的是什么?

正如您在我的代码中看到的那样,我已尽我所能来实现这些解决方案,但无济于事。

要记住的事情:我在 Galaxy Nexus 上测试这个,所以没有 SD 卡。只是云中的内部存储和音乐。我需要它在任何场景(内部、外部或云)中工作。它目前在这些都不起作用。

//@SuppressWarnings ("serial)")
public class CreationActivity extends Activity {
private final String [] STAR= {"*"};
//reads in all songs to an array


@Override
public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //set layout view and assign to variable
    setContentView(R.layout.creation);
    TableLayout myLayout = (TableLayout)findViewById(R.id.creationLayout);

    try {
        Bundle extras = getIntent().getExtras();

        if (extras!=null){
            //get the desired playlist and ID
            String playlist = extras.getString("playlist");
            Long playlistID = extras.getLong("playlistID");
            ArrayList<song> songs = new ArrayList<song>();

            //read in the songs from the playlist
            String[] proj = {MediaStore.Audio.Playlists.Members.TITLE,
                    MediaStore.Audio.Playlists.Members.ARTIST,
                    MediaStore.Audio.Playlists.Members.DURATION};

            //method 1
            Cursor songCursor = getContentResolver().query(MediaStore.Audio.Playlists.Members.getContentUri(null,playlistID),
                    proj,
                    null,
                    null,
                    null);

            //method 2
            /*
            Cursor songCursor = getContentResolver().query(Uri.parse("content://com.google.android.music.MusicContent/playlists/members"),
                   proj,
                    null,
                    null,
                    null);
            */

            //method 3
            /*
            Uri membersUri = MediaStore.Audio.Playlists.Members.getContentUri("internal", playlistID);
            Cursor membersCursor = managedQuery(membersUri, STAR, null, null, null);
            */

            //then this part with methods 1 and 2
            /*
            if (songCursor.getCount() > 0) {
                songCursor.moveToFirst();
                do {
                    song currSong = new song();
                    currSong.title = songCursor.getString(0);
                    currSong.artist = songCursor.getString(1);
                    songs.add(currSong);
                } while (songCursor.moveToNext());
            }
            songCursor.close();
            */

            //or this part with method 3
        /*
            membersCursor.moveToFirst();
            for(int s= 0; s<membersCursor.getCount(); s++,                           
                      membersCursor.moveToNext()){
                  song currSong = new song();
                      currSong.title = songCursor.getString(0);
              currSong.artist = songCursor.getString(1);
          songs.add(currSong);
            }
            membersCursor.close();
            */

        }else{
            Toast.makeText(getBaseContext(), "No songs",Toast.LENGTH_LONG).show();
        }
    } catch (NumberFormatException e){
    }
}
}   

编译过程中没有错误。但“可惜音乐App意外退出了”。每次。

谢谢您的帮助!

4

1 回答 1

4

我想到了。关键是将播放列表 ID 用作 URI 中的字符串。请参阅下面的代码。

这是获取播放列表名称和 ID 的部分:

String[] proj = {MediaStore.Audio.Playlists.NAME, MediaStore.Audio.Playlists._ID};
    Uri playlistUri = Uri.parse("content://com.google.android.music.MusicContent/playlists");
    Cursor playlistCursor = getContentResolver().query(playlistUri, proj, null, null, null);
    if (playlistCursor.getCount() > 0) {
        playlistCursor.moveToFirst();
        do {
            nameList.add(playlistCursor.getString(0));
            idList.add(playlistCursor.getLong(1));
        } while (playlistCursor.moveToNext());
    }

然后,一旦您拥有播放列表 ID,您就可以查询播放列表中的歌曲。这是实际查询信息并将其全部放入数组列表的代码部分。注意:“song”是我在别处定义的类,其中 readSong 是一种将值分配给各种值(标题、艺术家等)的方法。

ArrayList<song> songs = new ArrayList<song>();

            //read songs into library from the correct playlist
            String[] proj = {MediaStore.Audio.Playlists.Members.TITLE, MediaStore.Audio.Playlists.Members.ARTIST, MediaStore.Audio.Playlists.Members.DURATION, MediaStore.Audio.Playlists.Members._ID};
            Uri songUri = Uri.parse("content://com.google.android.music.MusicContent/playlists/" + playlistID + "/members");
            Cursor songCursor = getContentResolver().query(songUri, proj, null, null, null);
            if (songCursor.getCount() > 0) {
                songCursor.moveToFirst();
                do {
                    //create dummy song
                    song currSong = new song();
                    //read info to dummy var
                    currSong.readSong(songCursor);
                    //add instance to collection
                    songs.add(currSong);
                } while (songCursor.moveToNext());
            }
            songCursor.close();

我希望这可以帮助其他为此苦苦挣扎的人!如果您对我的方法或改进方法有任何意见,请告诉我!

于 2012-08-18T16:45:54.870 回答