2

I'm trying to display a list of songs from a playlist. I was able to get a list of songs, however when I play it, it doesn't work.

It seems it is not getting the right the MediaStore.Audio.Media._ID.

Here's the code on how I display the list:

private ArrayList<Song> listAllSongsByPlaylistId(long playlistId){
    Cursor cursor;
    ArrayList<Song> songList = new ArrayList<Song>();
    ContentResolver musicResolver = getActivity().getContentResolver();

    Uri allSongsUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    if (isSdPresent()) {
        cursor = musicResolver.query(allSongsUri, null, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    //Log.d("KO Genres:",cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Genres.NAME))+":"+genres);
                    Song song = new Song();

                    String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    String[] res = data.split("\\.");
                    song.setSongName(res[0]);
                    song.setSongFullPath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
                    song.setSongId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));
                    song.setSongAlbumName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
                    song.setArtist(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
                    song.setSongUri(ContentUris.withAppendedId(
                            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                            cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID))));
                    String duration = String.valueOf(getDuration(cursor.getLong(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION))));
                    song.setSongDuration(duration);

                    songList.add(song);

                } while (cursor.moveToNext());

                return songList;
            }
            cursor.close();
        }
    }
    return null;
}

I have another method which works fine and play song correctly, it display all songs.

I checked the MediaStore.Audio.Media._ID, the same songs from both method has a different MediaStore.Audio.Media._ID, and that seems to be causing the problem.

private ArrayList<Song> listAllSongs() { //Fetch path to all the files from internal & external storage n store it in songList
    Cursor cursor;
    ArrayList<Song> songList = new ArrayList<Song>();
    ContentResolver musicResolver = getActivity().getContentResolver();

    Uri allSongsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    if (isSdPresent()) {
        cursor = musicResolver.query(allSongsUri, null, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    Song song = new Song();

                    String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    String[] res = data.split("\\.");
                    song.setSongName(res[0]);
                    song.setSongFullPath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
                    song.setSongId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));
                    song.setSongAlbumName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
                    song.setArtist(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
                    song.setSongUri(ContentUris.withAppendedId(
                            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                            cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID))));
                    String duration = String.valueOf(getDuration(cursor.getLong(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION))));
                    song.setSongDuration(duration);
                    songList.add(song);
                } while (cursor.moveToNext());
                return songList;
            }
            cursor.close();
        }
    }
    return null;
}
Community
  • 1
  • 1
Julia
  • 1,207
  • 4
  • 29
  • 47
  • can you add the log cat? – Carlos Carrizales Nov 17 '15 at 18:48
  • Yah, sure. Everything else is the same except for the Media_ID. The display all list shows the id is 115, whereas in the playlist id, it is showing as 6. This is how the URI looks like content://media/external/audio/media/6 – Julia Nov 17 '15 at 19:16

0 Answers0