简体   繁体   中英

Android universal music player add album cover in the MediaItemViewHolder

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM