How do you access the name, artist, album name off the music stored on the phone?
Much like the musi开发者_如何学Goc app does I want to access the name of the song (not the name of the file) or the artist or album. For example, I want to populate a listview with the names of all the songs on the phone.
There are two approaches to this, one to read the meta tags from the music files themselves and one to use the MediaStore
content provider. MediaStore
is essentially a public database that you may query for all media related information on the phone.
Using MediaStore
is very simple and can be found in the docs here.
Here is a simple example from one of my applications:
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Artists.ARTIST };
tempCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
tempCursor.moveToFirst(); //reset the cursor
int col_index=-1;
int numSongs=tempCursor.getCount();
int currentNum=0;
do{
col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
artist = tempCursor.getString(col_index);
//do something with artist name here
//we can also move into different columns to fetch the other values
}
currentNum++;
}while(tempCursor.moveToNext());
精彩评论