与MediaStore获取专辑类型



我对光标如何在Android中工作非常新。目前,我能够检索专辑的名字和艺术家,但我无法检索它的流派。我环顾四周,找不到一种方法。我是否应该检查专辑中的所有歌曲,找到最常见的类型并基于该专辑的类型分配专辑类型,还是有一种更优雅的方法来做到这一点?通常,专辑中的所有歌曲都具有相同的类型,但并非总是如此。

谢谢。

您可以使用 MetaDataRetriever,如果需要的话...这是(根据我的经验)从歌曲中检索元数据的更完整的方法(您可以在链接中看到更多信息https https://。

  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 创建自定义类来解决Info ArrayAdapter来继承类结构并在listView中显示...也许是类似的东西(仅使用标题,band,band,专辑和歌曲存储的路径,外部存储在我的情况下,您可以按照这些行添加您想要的内容)

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

以下是我用来检索数据的功能(我使用索引我仅用于将所有歌曲的路径存储在字符串数组中,在另一个活动中使用它们,而对您无用)

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

最新更新