Android universal music player add album cover in

2019-04-14 14:56发布

问题:

Not sure how many people here are familiar with the Android Universal Music Player but I am having issue with displaying an album in the MediaItemViewHolder.java file.

So here is a basic structure after my modifications:

// image view for the album cover
holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);

// get the album art url
String artUrl = description.getIconUri().toString();
Bitmap art;
AlbumArtCache cache = AlbumArtCache.getInstance();
art = cache.getIconImage(artUrl);
....
if (cachedState == null || cachedState != state) {
    switch (state) {
    case STATE_PLAYABLE:
         // display the album cover
         holder.mImageView.setImageBitmap(art);
         break;
....

This correctly displays the album cover. However, it is initially blank. Once the user clicks on an item, the image is displayed.

Screenshot #1 : Once the app is loaded and user did not click on any item:

Screenshot #2 : Once the user click on the item to play the song

I am not really sure what is causing the album to be initially blank. Looking at the AlbumArtCache.java I can't see any restrictions about OnClickListener that can cause this.

Any suggestions how to resolve this issue?

回答1:

cache.getIconImage(url) does not actually fetch the url and store it in the cache. It returns the current value or null. Instead you have to call AlbumArtCache#fetch(String url, FetchListener listener)

A good prototype of this is in FullScreenPlayerActivity#fetchImageAsync(MediaDescription description)

Here is what you could do in a method when the item is playable.

AlbumArtCache cache = AlbumArtCache.getInstance();
Bitmap art = cache.getIconImage(url);
if (art == null) {
  cache.fetch(url, new AlbumArtCache.FetchListener() {
            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                if (artUrl.equals(url)) {
                    holder.mImageView.setImageBitmap(icon);
                }
            }
        });
} else {
    holder.mImageView.setImageBitmap(bitmap);
}