2

我正在制作一个音乐应用程序。到目前为止一切都很好。我想在播放歌曲的同时显示专辑封面。但我无法弄清楚这样做的方法。

4

3 回答 3

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

    }
}
于 2016-08-31T08:47:28.457 回答
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;
}
于 2016-08-29T15:11:17.530 回答
0

您可以使用Picasso,它使加载图像变得超级容易,并且有一些不错的选择。

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

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

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

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

于 2016-08-29T15:21:18.640 回答