我对游戏在Android中的工作方式非常了解。目前,我能够检索专辑的名称和艺术家,但我无法检索它的类型。我环顾四周,无法找到办法。我应该检查专辑中的所有歌曲,找到最常见的类型并根据它分配专辑类型,还是有更优雅的方式来做到这一点?通常,专辑中的所有歌曲都具有相同的类型,但情况并非总是如此。
谢谢。
你可以使用MetaDataRetriever
...这是(根据我的经验)一种更完整的方式从歌曲中检索元数据(你可以在链接https://developer.android.com/reference/android/media/MediaMetadataRetriever.html中看到更多)...我在我的一个项目中写了这样的东西,希望这可以帮到你:
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
}
}
更新:好吧,如果我理解正确,你想要在列表中显示每首歌曲的标题,专辑,乐队,流派等信息吗?我通常使用ListView ...你可以使用ListView +创建一个自定义类来解决信息+一个ArrayAdapter继承类结构并在ListView中显示...也许类似(只是使用标题,乐队,专辑和存储歌曲的路径,在我的情况下外部存储,你可以添加你想要的这些行)
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;
}
songs adapter.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;
}
}
以下是我用来检索数据的函数(我使用索引来存储字符串数组中的所有歌曲的路径,在另一个活动中使用它们,对你没用)
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());
}
}
最后一个函数用于显示最后在前一个函数中收集的所有数据
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);
}
});
}