3

Big picture: GUI shows user a list of their playlists. User picks one. Program passes chosen playlist to next activity which displays the songs in that playlist.

Problem: I can display the playlists and register the users choice, but I can't seem to display the songs of that play list.

Yes, I've see the following questions:

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?

As you can see in my code, I've done my best to implement those solutions, but to no avail.

Things to keep in mind: I'm testing this on a Galaxy Nexus, so no SDcard. Just internal storage and music in the cloud. I need it to work in any scenario (internal, external, or cloud). It currently works in none of those.

//@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){
    }
}
}   

No errors during compiling. But "Unfortunately Music App has unexpectedly quit." every time.

Thanks for the help!

Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
Scott Decker
  • 4,229
  • 7
  • 24
  • 39
  • Have you tried debugging? Seeing what the values of your variables are? Please do this and tell us what the issue is. It's rather hard to help you when you all you say is "Unfortunately Music App has unexpectedly quit". That's like telling your doctor just this and nothing more "Something is troubling me". Be specific. – A Random Android Dev Aug 06 '12 at 23:35
  • Will do...it'll be a couple more days, but I'll get back to you. – Scott Decker Aug 11 '12 at 03:42

1 Answers1

4

I figured it out. The key was to use the playlist ID as a string immediately within the URI. See code below.

This is the part that will get the playlist names and IDs:

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());
    }

Then once you have the playlist ID you can query for the songs in the playlist. This is the part of code that actually queries for the info and puts it all in an array list. NOTE: "song" is a class I have defined elsewhere, where readSong is a method that assigns values to various values (title, artist, etc).

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();

I hope this helps anybody else who was struggling with this!! Let me know if you have any comments on my method or ways to make it better!

Scott Decker
  • 4,229
  • 7
  • 24
  • 39