Search in sources :

Example 1 with AlbumArtCache

use of com.example.android.uamp.AlbumArtCache in project android-UniversalMusicPlayer by googlesamples.

the class PlaybackControlsFragment method onMetadataChanged.

private void onMetadataChanged(MediaMetadataCompat metadata) {
    LogHelper.d(TAG, "onMetadataChanged ", metadata);
    if (getActivity() == null) {
        LogHelper.w(TAG, "onMetadataChanged called when getActivity null," + "this should not happen if the callback was properly unregistered. Ignoring.");
        return;
    }
    if (metadata == null) {
        return;
    }
    mTitle.setText(metadata.getDescription().getTitle());
    mSubtitle.setText(metadata.getDescription().getSubtitle());
    String artUrl = null;
    if (metadata.getDescription().getIconUri() != null) {
        artUrl = metadata.getDescription().getIconUri().toString();
    }
    if (!TextUtils.equals(artUrl, mArtUrl)) {
        mArtUrl = artUrl;
        Bitmap art = metadata.getDescription().getIconBitmap();
        AlbumArtCache cache = AlbumArtCache.getInstance();
        if (art == null) {
            art = cache.getIconImage(mArtUrl);
        }
        if (art != null) {
            mAlbumArt.setImageBitmap(art);
        } else {
            cache.fetch(artUrl, new AlbumArtCache.FetchListener() {

                @Override
                public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                    if (icon != null) {
                        LogHelper.d(TAG, "album art icon of w=", icon.getWidth(), " h=", icon.getHeight());
                        if (isAdded()) {
                            mAlbumArt.setImageBitmap(icon);
                        }
                    }
                }
            });
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) AlbumArtCache(com.example.android.uamp.AlbumArtCache)

Example 2 with AlbumArtCache

use of com.example.android.uamp.AlbumArtCache in project android-UniversalMusicPlayer by googlesamples.

the class FullScreenPlayerActivity method fetchImageAsync.

private void fetchImageAsync(@NonNull MediaDescriptionCompat description) {
    if (description.getIconUri() == null) {
        return;
    }
    String artUrl = description.getIconUri().toString();
    mCurrentArtUrl = artUrl;
    AlbumArtCache cache = AlbumArtCache.getInstance();
    Bitmap art = cache.getBigImage(artUrl);
    if (art == null) {
        art = description.getIconBitmap();
    }
    if (art != null) {
        // if we have the art cached or from the MediaDescription, use it:
        mBackgroundImage.setImageBitmap(art);
    } else {
        // otherwise, fetch a high res version and update:
        cache.fetch(artUrl, new AlbumArtCache.FetchListener() {

            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                // the previous hasn't yet returned:
                if (artUrl.equals(mCurrentArtUrl)) {
                    mBackgroundImage.setImageBitmap(bitmap);
                }
            }
        });
    }
}
Also used : Bitmap(android.graphics.Bitmap) AlbumArtCache(com.example.android.uamp.AlbumArtCache)

Example 3 with AlbumArtCache

use of com.example.android.uamp.AlbumArtCache in project android-UniversalMusicPlayer by googlesamples.

the class CardViewHolder method setupCardView.

/**
 * Set the view in this holder to represent the media metadata in {@code description}
 */
public void setupCardView(final Context context, MediaDescriptionCompat description) {
    mCardView.setTitleText(description.getTitle());
    mCardView.setContentText(description.getSubtitle());
    mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
    // Based on state of item, set or unset badge
    Drawable drawable = MediaItemViewHolder.getDrawableByState(context, mItemState);
    mCardView.setBadgeImage(drawable);
    Uri artUri = description.getIconUri();
    if (artUri == null) {
        setCardImage(context, description.getIconBitmap());
    } else {
        // IconUri potentially has a better resolution than iconBitmap.
        String artUrl = artUri.toString();
        AlbumArtCache cache = AlbumArtCache.getInstance();
        if (cache.getBigImage(artUrl) != null) {
            // So, we use it immediately if it's cached:
            setCardImage(context, cache.getBigImage(artUrl));
        } else {
            // Otherwise, we use iconBitmap if available while we wait for iconURI
            setCardImage(context, description.getIconBitmap());
            cache.fetch(artUrl, new AlbumArtCache.FetchListener() {

                @Override
                public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                    setCardImage(context, bitmap);
                }
            });
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) AlbumArtCache(com.example.android.uamp.AlbumArtCache) BitmapDrawable(android.graphics.drawable.BitmapDrawable) AnimationDrawable(android.graphics.drawable.AnimationDrawable) Drawable(android.graphics.drawable.Drawable) Uri(android.net.Uri)

Aggregations

Bitmap (android.graphics.Bitmap)3 AlbumArtCache (com.example.android.uamp.AlbumArtCache)3 AnimationDrawable (android.graphics.drawable.AnimationDrawable)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1