Example usage for android.media MediaMetadata METADATA_KEY_ALBUM_ART

List of usage examples for android.media MediaMetadata METADATA_KEY_ALBUM_ART

Introduction

In this page you can find the example usage for android.media MediaMetadata METADATA_KEY_ALBUM_ART.

Prototype

String METADATA_KEY_ALBUM_ART

To view the source code for android.media MediaMetadata METADATA_KEY_ALBUM_ART.

Click Source Link

Document

The artwork for the album of the media's original source as a Bitmap .

Usage

From source file:uk.org.ngo.squeezer.service.SqueezeService.java

/**
 * Manages the state of any ongoing notification based on the player and connection state.
 *//*from  w w w  .java2 s  . c om*/
private void updateOngoingNotification() {
    Player activePlayer = this.mActivePlayer.get();
    PlayerState activePlayerState = getActivePlayerState();

    // Update scrobble state, if either we're currently scrobbling, or we
    // were (to catch the case where we started scrobbling a song, and the
    // user went in to settings to disable scrobbling).
    if (scrobblingEnabled || scrobblingPreviouslyEnabled) {
        scrobblingPreviouslyEnabled = scrobblingEnabled;
        Scrobble.scrobbleFromPlayerState(this, activePlayerState);
    }

    // If there's no active player then kill the notification and get out.
    // TODO: Have a "There are no connected players" notification text.
    if (activePlayer == null || activePlayerState == null) {
        clearOngoingNotification();
        return;
    }

    boolean playing = activePlayerState.isPlaying();

    // If the song is not playing and the user wants notifications only when playing then
    // kill the notification and get out.
    if (!playing && !mShowNotificationWhenNotPlaying) {
        clearOngoingNotification();
        return;
    }

    // If there's no current song then kill the notification and get out.
    // TODO: Have a "There's nothing playing" notification text.
    final Song currentSong = activePlayerState.getCurrentSong();
    if (currentSong == null) {
        clearOngoingNotification();
        return;
    }

    // Compare the current state with the state when the notification was last updated.
    // If there are no changes (same song, same playing state) then there's nothing to do.
    String songName = currentSong.getName();
    String albumName = currentSong.getAlbumName();
    String artistName = currentSong.getArtist();
    Uri url = currentSong.getArtworkUrl();
    String playerName = activePlayer.getName();

    if (mNotifiedPlayerState == null) {
        mNotifiedPlayerState = new PlayerState();
    } else {
        boolean lastPlaying = mNotifiedPlayerState.isPlaying();
        Song lastNotifiedSong = mNotifiedPlayerState.getCurrentSong();

        // No change in state
        if (playing == lastPlaying && currentSong.equals(lastNotifiedSong)) {
            return;
        }
    }

    mNotifiedPlayerState.setCurrentSong(currentSong);
    mNotifiedPlayerState.setPlayStatus(activePlayerState.getPlayStatus());
    final NotificationManagerCompat nm = NotificationManagerCompat.from(this);

    PendingIntent nextPendingIntent = getPendingIntent(ACTION_NEXT_TRACK);
    PendingIntent prevPendingIntent = getPendingIntent(ACTION_PREV_TRACK);
    PendingIntent playPendingIntent = getPendingIntent(ACTION_PLAY);
    PendingIntent pausePendingIntent = getPendingIntent(ACTION_PAUSE);
    PendingIntent closePendingIntent = getPendingIntent(ACTION_CLOSE);

    Intent showNowPlaying = new Intent(this, NowPlayingActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, showNowPlaying, 0);
    Notification notification;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        final Notification.Builder builder = new Notification.Builder(this);
        builder.setContentIntent(pIntent);
        builder.setSmallIcon(R.drawable.squeezer_notification);
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
        builder.setShowWhen(false);
        builder.setContentTitle(songName);
        builder.setContentText(albumName);
        builder.setSubText(playerName);
        builder.setStyle(new Notification.MediaStyle().setShowActionsInCompactView(1, 2)
                .setMediaSession(mMediaSession.getSessionToken()));

        final MediaMetadata.Builder metaBuilder = new MediaMetadata.Builder();
        metaBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, artistName);
        metaBuilder.putString(MediaMetadata.METADATA_KEY_ALBUM, albumName);
        metaBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, songName);
        mMediaSession.setMetadata(metaBuilder.build());

        // Don't set an ongoing notification, otherwise wearable's won't show it.
        builder.setOngoing(false);

        builder.setDeleteIntent(closePendingIntent);
        if (playing) {
            builder.addAction(
                    new Notification.Action(R.drawable.ic_action_previous, "Previous", prevPendingIntent))
                    .addAction(new Notification.Action(R.drawable.ic_action_pause, "Pause", pausePendingIntent))
                    .addAction(new Notification.Action(R.drawable.ic_action_next, "Next", nextPendingIntent));
        } else {
            builder.addAction(
                    new Notification.Action(R.drawable.ic_action_previous, "Previous", prevPendingIntent))
                    .addAction(new Notification.Action(R.drawable.ic_action_play, "Play", playPendingIntent))
                    .addAction(new Notification.Action(R.drawable.ic_action_next, "Next", nextPendingIntent));
        }

        ImageFetcher.getInstance(this).loadImage(url,
                getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
                getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height),
                new ImageWorker.ImageWorkerCallback() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                    public void process(Object data, @Nullable Bitmap bitmap) {
                        if (bitmap == null) {
                            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_album_noart);
                        }

                        metaBuilder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap);
                        metaBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART, bitmap);
                        mMediaSession.setMetadata(metaBuilder.build());
                        builder.setLargeIcon(bitmap);
                        nm.notify(PLAYBACKSERVICE_STATUS, builder.build());
                    }
                });
    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        builder.setOngoing(true);
        builder.setCategory(NotificationCompat.CATEGORY_SERVICE);
        builder.setSmallIcon(R.drawable.squeezer_notification);

        RemoteViews normalView = new RemoteViews(this.getPackageName(), R.layout.notification_player_normal);
        RemoteViews expandedView = new RemoteViews(this.getPackageName(),
                R.layout.notification_player_expanded);

        normalView.setOnClickPendingIntent(R.id.next, nextPendingIntent);

        expandedView.setOnClickPendingIntent(R.id.previous, prevPendingIntent);
        expandedView.setOnClickPendingIntent(R.id.next, nextPendingIntent);

        builder.setContent(normalView);

        normalView.setTextViewText(R.id.trackname, songName);
        normalView.setTextViewText(R.id.albumname, albumName);

        expandedView.setTextViewText(R.id.trackname, songName);
        expandedView.setTextViewText(R.id.albumname, albumName);
        expandedView.setTextViewText(R.id.player_name, playerName);

        if (playing) {
            normalView.setImageViewResource(R.id.pause, R.drawable.ic_action_pause);
            normalView.setOnClickPendingIntent(R.id.pause, pausePendingIntent);

            expandedView.setImageViewResource(R.id.pause, R.drawable.ic_action_pause);
            expandedView.setOnClickPendingIntent(R.id.pause, pausePendingIntent);
        } else {
            normalView.setImageViewResource(R.id.pause, R.drawable.ic_action_play);
            normalView.setOnClickPendingIntent(R.id.pause, playPendingIntent);

            expandedView.setImageViewResource(R.id.pause, R.drawable.ic_action_play);
            expandedView.setOnClickPendingIntent(R.id.pause, playPendingIntent);
        }

        builder.setContentTitle(songName);
        builder.setContentText(getString(R.string.notification_playing_text, playerName));
        builder.setContentIntent(pIntent);

        notification = builder.build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notification.bigContentView = expandedView;
        }

        nm.notify(PLAYBACKSERVICE_STATUS, notification);

        ImageFetcher.getInstance(this).loadImage(this, url, normalView, R.id.album,
                getResources().getDimensionPixelSize(R.dimen.album_art_icon_normal_notification_width),
                getResources().getDimensionPixelSize(R.dimen.album_art_icon_normal_notification_height), nm,
                PLAYBACKSERVICE_STATUS, notification);
        ImageFetcher.getInstance(this).loadImage(this, url, expandedView, R.id.album,
                getResources().getDimensionPixelSize(R.dimen.album_art_icon_expanded_notification_width),
                getResources().getDimensionPixelSize(R.dimen.album_art_icon_expanded_notification_height), nm,
                PLAYBACKSERVICE_STATUS, notification);
    }
}

From source file:com.av.remusic.service.MediaService.java

private void updateMediaSession(final String what) {
    int playState = mIsSupposedToBePlaying ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_PAUSED;

    if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setPlaybackState(new PlaybackState.Builder().setState(playState, position(), 1.0f)
                    .setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE
                            | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT
                            | PlaybackState.ACTION_SKIP_TO_PREVIOUS)
                    .build());/*from w  ww.j ava  2s  .com*/
        }
    } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) {
        //Bitmap albumArt = ImageLoader.getInstance().loadImageSync(CommonUtils.getAlbumArtUri(getAlbumId()).toString());
        Bitmap albumArt = null;
        if (albumArt != null) {

            Bitmap.Config config = albumArt.getConfig();
            if (config == null) {
                config = Bitmap.Config.ARGB_8888;
            }
            albumArt = albumArt.copy(config, false);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setMetadata(
                    new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_ARTIST, getArtistName())
                            .putString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName())
                            .putString(MediaMetadata.METADATA_KEY_ALBUM, getAlbumName())
                            .putString(MediaMetadata.METADATA_KEY_TITLE, getTrackName())
                            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration())
                            .putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1)
                            .putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, getQueue().length)
                            .putString(MediaMetadata.METADATA_KEY_GENRE, getGenreName())
                            .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART,
                                    mShowAlbumArtOnLockscreen ? albumArt : null)
                            .build());

            mSession.setPlaybackState(new PlaybackState.Builder().setState(playState, position(), 1.0f)
                    .setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE
                            | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT
                            | PlaybackState.ACTION_SKIP_TO_PREVIOUS)
                    .build());
        }
    }
}