I'm making a music player and it was difficult to get information about the songs.
I'm using a ContentResolver and a Cursor to do the music searches in the device's memory. The information like name, title, artist and others and I can capture normal, since the Cursor itself returns this information, with the exception of the Album.
The problem is to capture the Genre (musical style). For this I use the MediaMetadataRetriever, however it is returning strange values.
In the case of the Album, the information is captured with the Cursor, but when the file does not have a saved album name, it returns the musical genre.
In the case of Genre, it returns numbers in some musical styles, for example: When it is a hip-hop it returns (5), when rap, returns (7) and so on.
The question is: How do I get this information correctly? Below is the code for reading songs
private void lerMusicas() {
ContentResolver musicResolver = getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int nameColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
int artistColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int albumColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int folderColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisAlbumArtist = "";
String thisAlbum = musicCursor.getString(albumColumn);
String thisName = musicCursor.getString(nameColumn);
Uri thisUri = ContentUris.withAppendedId(musicUri, thisId);
String thisGender = "";
String thisFolder = musicCursor.getString(folderColumn);
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(MainActivity.this, thisUri);
if (retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE) != null)
thisGender = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
else thisGender = "<unknown>";
if (retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST) != null)
thisAlbumArtist = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);
else thisAlbumArtist = "<unknown>";
retriever.release();
} catch (RuntimeException ex) {
// something went wrong with the file, ignore it and continue
}
todasMusicas.add(new Music(thisId, thisName, thisTitle, thisArtist, thisAlbumArtist, thisAlbum, thisGender, thisFolder, thisUri));
}
while (musicCursor.moveToNext());
}
}
Ps: In a ViewPager, the information is displayed in a ListView, where only one information is displayed per Fragment.