0
I’m making a music player and appeared a difficulty to capture the information of the songs.
I’m using a Contentresolver and a Cursor to search for songs in the device’s memory. The information such as name, title, artist and others and I can capture normal, since Cursor itself returns this information, with the exception of Album.
The problem is to capture the Genre(musical style). For this I use Mediametadataretriever, but it is returning strange values.
In the case of Album, the information is captured with 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 capture this information correctly? Below follows the code of reading the 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 by Fragment.
https://stackoverflow.com/questions/3728921/get-the-genre-of-a-song/19743031
– Giuliana Bezerra
not all files have full metadata, this information totally depends on the author(or third party) entering this information in the audio
– ederwander