【问题标题】:How to display album art in my music player(Android)?如何在我的音乐播放器(Android)中显示专辑封面?
【发布时间】:2017-01-05 15:53:18
【问题描述】:

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

【问题讨论】:

  • 您可以使用像Universal Image Loader 或Glide 这样的图像加载库。传递专辑封面的 URI 并将其加载到 ImageView。它应该可以轻松加载您的专辑封面。

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


【解决方案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);

    }
}

【讨论】:

    【解决方案2】:

    可以使用以下函数(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;
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多