Reputation: 710
I'm very new to how cursors work in Android. Currently, I am able to retrieve an album's name and artist, but I am not able to retrieve it's genre. I've looked around and cannot find a way to do this. Should I just check over all the songs in the album, find the most common genre and assign the album genre based on that, or is there a more elegant way to do this? Typically all songs in an album will have the same genre, but this is not always the case.
Thanks.
Upvotes: 0
Views: 701
Reputation: 520
You can use MetaDataRetriever
if you want...It's (in my experience) a more complete way to retrieve metadata from a song (you can see more in the link https://developer.android.com/reference/android/media/MediaMetadataRetriever.html )...I wrote something like this in a project of mine, hope this can help you:
public void MetSongRetriever() {
//MediaPlayerPlay is the context, I shared a function i wrote myself
//Just like myUri is the path of the song (external storage in my case)
mMediaData.setDataSource(MediaPlayerPlay.this, myUri);
try {
mNameSong.setText(mMediaData.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
mBandName.setText(mMediaData.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
art = metaRetriver.getEmbeddedPicture();
Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
mCoverImage.setImageBitmap(songImage);
mAlbumName.setText(metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
mGenre.setText(metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
}
catch (Exception e) {
//set all the text Uknown, like "Unknown title" and so on
//And the background for the image grey
}
}
UPDATE: Well, if I understood correctly, you want to show info like title, album, band, genre and so on of each songs in a list right? I usually use a ListView for that...you can use ListView + creating a custom class to settle the info + an ArrayAdapter to inherit the class structure and show it in a ListView...maybe something like (just using title, band, album and the path where the song is stored, external storage in my case, you can add what you want following these lines)
Songs.java
public class Songs {
public String mSongname, mBandName, mAlbumName, mPathSong;
public Songs(String songName, String bandName, String albumName, String pathSong) {
mSongname = songName;
mBandName = bandName;
mAlbumName = albumName;
mPathSong = pathSong;
}
public void setSongname(String songname) {
mSongname = songname;
}
public void setBandName(String bandName) {
mBandName = bandName;
}
public void setAlbumName(String albumName) {
mAlbumName = albumName;
}
public void setPathSong(String pathSong) {
mPathSong = pathSong;
}
public String getSongname() {
return mSongname;
}
public String getBandName() {
return mBandName;
}
public String getAlbumName() {
return mAlbumName;
}
public String getPathSong() {
return mPathSong;
}
SongsAdapter.java
public class SongsAdapter extends ArrayAdapter<Songs> {
public SongsAdapter(Context context, ArrayList<Songs> mySongs) {
super(context, 0, mySongs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Storing the data from the current position
Songs mySongs = getItem(position);
//Check if the convertview is reused, otherwise i load it
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_songs, parent, false);
}
//Filling the structure with data
TextView SongTitle = (TextView) convertView.findViewById(R.id.ID_item_songs_title);
TextView BandName = (TextView) convertView.findViewById(R.id.ID_item_songs_band);
TextView AlbumName = (TextView) convertView.findViewById(R.id.ID_item_songs_album);
TextView PathName = (TextView) convertView.findViewById(R.id.ID_item_songs_path);
//Inserting the data in the template view
SongTitle.setText(mySongs.mSongname);
BandName.setText(mySongs.mBandName);
AlbumName.setText(mySongs.mAlbumName);
PathName.setText(mySongs.mPathSong);
//Return of the view for visualisation purpose
return convertView;
}
}
The following one is the function I used to retrieve the data (i used the index i just for storing all the songs' paths in a string array, to use them in another activity, not usefull for you)
public void getMusicList() {
int i = 0;
//I used the content resolver to get access to another part of the device, in my case the external storage
ContentResolver mContentResolver = getContentResolver();
//Taking the external storage general path
Uri mSongUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//The cursor is used to point to a certain row in the temp database, so you can store just parts of it and not the whole database (efficiency maxed)
Cursor mSongCursor = getContentResolver().query(mSongUri, null, null, null, null, null);
if ((mSongCursor != null) && mSongCursor.moveToFirst()) {
//Gathering the index of each column for each info i want
int mTempSongTitle = mSongCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int mTempBand = mSongCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int mTempAlbum = mSongCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int mTempLocationPath = mSongCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do {
//Gathering the real info based on the index
String mCurrentTitle = mSongCursor.getString(mTempSongTitle);
String mCurrentBand = mSongCursor.getString(mTempBand);
String mCurrentAlbum = mSongCursor.getString(mTempAlbum);
String mCurrentPath = mSongCursor.getString(mTempLocationPath);
mListOfPaths[i] = mCurrentPath;
i++;
Songs newSong = new Songs(mCurrentTitle, mCurrentBand, mCurrentAlbum, mCurrentPath);
adapter.add(newSong);
} while (mSongCursor.moveToNext());
}
}
And this last function is used to show finally all the data gathered in previous function
public void doStuff() {
mSongNames = (ListView) findViewById(R.id.ID_MPM_listView);
mSongNames.setAdapter(adapter);
getMusicList();
//The event it will occur when you click on an item of the listView
mSongNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent mMediaPlayerPlayIntent = new Intent(MediaPlayerMain.this, MediaPlayerPlay.class);
mMediaPlayerPlayIntent.putExtra("ExtraPosition", position);
mMediaPlayerPlayIntent.putExtra("StringArray", mListOfPaths);
startActivity(mMediaPlayerPlayIntent);
}
});
}
Upvotes: 1