3

I am currently creating a music player and I am retrieving the music on the device using a Cursor as such

mCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
            requestedColumns, null, null, null);

where

private String[] requestedColumns = 
{
    MediaStore.Audio.Media.TITLE,
    MediaStore.Audio.Media._ID,
    MediaStore.Audio.Media.DATA,
    MediaStore.Audio.Media.ALBUM,
    MediaStore.Audio.Media.ALBUM_ID,
    MediaStore.Audio.Media.ARTIST,
    MediaStore.Audio.Media.ARTIST_ID,
    MediaStore.Audio.Media.YEAR,
    MediaStore.Audio.Media.DURATION,        
};

Now this is working perfectly and I am successfully retrieving music on my device/SD Card. My questions is: is there any way to get the Album artist for a particular song? Now I don't simply mean the artist of a partcular the song, but the main artist behind the entire album from which a song belongs. There does not seem to be some kind of MediaStore.Audio.Media.ALBUM_ARTIST.

On a side note: what is the difference (if any) between using MediaStore.Audio.Media.ALBUM and MediaStore.Audio.ALBUMS?

Kent Hawkings
  • 2,793
  • 2
  • 25
  • 30

3 Answers3

2

I think what you are looking for is MediaStore.Audio.Albums.ARTIST, which you can look up as the second part of a two-stage process.

Cursor cursor = getActivity().managedQuery(
    MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
    new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ARTIST}, 
    MediaStore.Audio.Albums._ID+ "=?", 
    new String[] {albumId}, 
    null);

Where albumId is the album id of the song, ie what you have gotten in your earlier query: MediaStore.Audio.Media.ALBUM_ID

Think of MediaStore.Audio.ALBUMS as metadata about all your albums. MediaStore.Audio.Media.ALBUM refers to the album this audio file is from. Media is always metadata about each individual file.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • 1
    Seems like this is just returning the Artist of the first song of the album somehow. Is there really no way to get the Album-artist i declared in iTunes for example? – Dominik Seemayr Mar 03 '16 at 15:08
  • 1
    This may or may not be an issue any more depending on what version of Android you are using: https://code.google.com/p/android/issues/detail?id=52309. Alternatively you could skip the MediaStore and manually read the ID3 tags yourself – Ken Wolf Mar 03 '16 at 15:48
  • It happens for me even I develop for minSDK 21. Is reading them manually as fast as the MediaStore? – Dominik Seemayr Mar 03 '16 at 17:49
  • 1
    Probably not as fast but I doubt it would be an issue. http://developer.android.com/reference/android/media/MediaMetadataRetriever.html#METADATA_KEY_ALBUMARTIST – Ken Wolf Mar 03 '16 at 17:55
  • ahh, tried it with MediaMetadataRetriever and it took ages to get the AlbumArtist out of 8000 Songs. Maybe i did something wrong. Do Apps like google music work with it? – Dominik Seemayr Mar 03 '16 at 22:54
  • No idea I'm afraid, it's closed source. Good luck! – Ken Wolf Mar 04 '16 at 06:26
1

This works. Do not ask me how though !!

   public Cursor getAlbumArtistCursor(Context context) {
    ContentResolver cr = context.getContentResolver();
    // query all media and get the album_artist field
    return cr.query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[]{  "album_artist", MediaStore.Audio.Media.ALBUM_ID,  MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media._ID}, null, null, null);
}
Theo
  • 2,012
  • 1
  • 16
  • 29
0

In my app I noticed the same problems and therefore ran a test with artificial metadata:

  • Album "Testalbum" with two tracks
  • First Track: Artist "Artist-A", Album-Artist "Test-Album-Artist".
  • Second Track: Artist "Artist-B", Album-Artist "Test-Album-Artist".

I tested with Android 4.4.4 (Cyanogen), Android 6.0.1 (Google) and Android 7.1.2 (AOSP, unofficial). I did not have access to Android 8.0 or 8.1.

Result: Android shows "Artist-A" as album artist. This is obviously incorrect.

There are two bugs in Android:

  • Android ignores the album artist tag.
  • Android does not compare all artists :If artists are different, Android is supposed to choose various, but it chooses any instead.

Further, the bug #52309 mentioned above has been closed long ago, with comment "obsolete". Even Google Play Music, the reference music player, also suffers from the same bugs, this is nonsense.

So what solutions do we have?

  1. Try to reopen the old bug report.
  2. Write a new bug report.
  3. Repair the behaviour in source of LineageOS or similar.
  4. "Live" with the bug.
  5. Read all tracks and use various in case they do not all match.
  6. Get the metadata independently from Android, e.g. using a tagger library.
  • Also i have noticed that in Android 8.0 i am getting contributing artists even when i am requesting artist from MediaMetadataRetriever.METADATA_KEY_ARTIST .No solution till now – Siva agarwal Apr 21 '19 at 01:02