如何获得"翻唱歌曲"(所以同一张专辑中的歌曲有相同的封面)?
// Retrieve song info from device
public void getSongList() {
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
// Query external audio
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, selection, null, null);
// Iterate over results if valid
if (musicCursor != null && musicCursor.moveToFirst()) {
// Get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
int durationColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.DURATION);
// Add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
long thisDuration = musicCursor.getLong(durationColumn);
String thisPathAlbumImage = ????
//**** HERE I WANT A PATH/URI WITH ALBUM SONG ****
arrayOfSongs.add(new Song(thisId, thisTitle, thisArtist, thisDuration, thisPathAlbumImage));
Log.d(LOG_TAG, "New song added: " + thisTitle);
}
while (musicCursor.moveToNext());
}
}
我需要另一个问题吗?我不想查询所有专辑,我不知道如何将歌曲连接到专辑。。。
您需要在上面的查询中包含相册id。
int albumIdColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ALBUM_ID);
long albumId = musicCursor.getLong(albumIdColumn);
使用相册id,您可以查询封面的路径,如下所示:
private static String getCoverArtPath(long albumId, Context context) {
Cursor albumCursor = context.getContentResolver().query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID + " = ?",
new String[]{Long.toString(albumId)},
null
);
boolean queryResult = albumCursor.moveToFirst();
String result = null;
if (queryResult) {
result = albumCursor.getString(0);
}
albumCursor.close();
return result;
}