使用 MediaStore 获取专辑类型

Get Album Genre with MediaStore

我对游标在 Android 中的工作方式还很陌生。目前,我能够检索到专辑的名称和艺术家,但无法检索到它的流派。我环顾四周,找不到办法做到这一点。我应该只检查专辑中的所有歌曲,找到最常见的流派并根据它分配专辑流派,还是有更优雅的方法来做到这一点?通常,专辑中的所有歌曲都具有相同的流派,但情况并非总是如此。

谢谢。

如果需要,您可以使用 MetaDataRetriever...这是(根据我的经验)从歌曲中检索元数据的更完整方法(您可以在 link 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 + 创建自定义 class 来解决信息 + ArrayAdapter 继承 class 结构并将其显示在 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;
}

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;
}
}

以下是我用来检索数据的函数(我使用索引 i 只是将所有歌曲的路径存储在一个字符串数组中,以便在另一个 activity 中使用它们,对你)

    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);

        }
    });
}