我正在尝试显示播放列表中的歌曲列表。我能够得到一个歌曲列表,但是当我播放它时,它不起作用。
似乎没有得到MediaStore.Audio.Media._ID
的权利。
以下是我如何显示列表的代码:
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;
}
我有另一种方法可以正常工作并正确播放歌曲,它会显示所有歌曲。
我检查了MediaStore.Audio.Media._ID
,两种方法中的相同歌曲都有不同的MediaStore.Audio.Media._ID
,这似乎导致了问题。
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;
}