如何从Google云或设备中读取歌曲

时间:2012-08-06 23:17:16

标签: android mediastore

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

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

是的,我看到以下问题:

How to query for songs in playlists on Android SDK?

Given an Android music playlist name, how can one find the songs in the playlist?

What is the String 'volumeName' argument of MediaStore.Audio.Playlists.Members.getContentUri referring to?

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

要记住的事情:我在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){
    }
}
}   

编译期间没有错误。但“不幸的是音乐应用程序出乎意料地退出了。”每一次。

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

我明白了。关键是在URI中立即使用播放列表ID作为字符串。请参阅下面的代码。

这是获取播放列表名称和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();

我希望这可以帮助任何正在努力解决这个问题的人!如果您对我的方法或改进方法有任何意见,请告诉我们。