如何在我的音乐播放器(Android)中显示专辑封面?

时间:2016-08-29 14:55:06

标签: android android-studio android-activity android-music-player albumart

我正在制作音乐应用。到目前为止,一切都很顺利。我想要播放专辑封面以及播放的歌曲。但我无法弄清楚这样做的方法。

3 个答案:

答案 0 :(得分:1)

您可以使用MediaMetaDataRetriever类从媒体文件中获取所有元数据。

代码如下:

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getInit();

        // Ablum_art retrieval code //

        metaRetriver = new MediaMetadataRetriever();
        metaRetriver.setDataSource("/sdcard/audio.mp3");
        try {
            art = metaRetriver.getEmbeddedPicture();
            Bitmap songImage = BitmapFactory
                    .decodeByteArray(art, 0, art.length);
            album_art.setImageBitmap(songImage);
            album.setText(metaRetriver
                    .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
            artist.setText(metaRetriver
                    .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
            genre.setText(metaRetriver
                    .extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
        } catch (Exception e) {
            album_art.setBackgroundColor(Color.GRAY);
            album.setText("Unknown Album");
            artist.setText("Unknown Artist");
            genre.setText("Unknown Genre");
        }

    }

    // Fetch Id's form xml 

    public void getInit() {

        album_art = (ImageView) findViewById(R.id.album_art);
        album = (TextView) findViewById(R.id.Album);
        artist = (TextView) findViewById(R.id.artist_name);
        genre = (TextView) findViewById(R.id.genre);

    }
}

答案 1 :(得分:0)

您可以使用以下功能(api> 10)

Bitmap getEmbeddedPicture(String songPath){
    android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(songsList.get(songIndex).get("songPath"));

        byte [] data = mmr.getEmbeddedPicture();
               //coverart is an Imageview object

        // convert the byte array to a bitmap
        if(data != null)
        {
           return BitmapFactory.decodeByteArray(data, 0, data.length);

        }
        else
            return null;
}

答案 2 :(得分:0)

您可以使用Picasso,它可以让您轻松加载图片并拥有一些不错的选项。

基本上,你只需要你的imageView(或所有这些并循环它们并加载每个图像)和图像的URI。

Picasso.with(context)
            .load(uri)
            .into(imageView);

链接到毕加索有趣而简单的教程:

http://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149

相关问题