Search in sources :

Example 1 with MediaDescriptionCompat

use of android.support.v4.media.MediaDescriptionCompat in project AntennaPod by AntennaPod.

the class CustomMRControllerDialog method fetchArt.

private Pair<Bitmap, Integer> fetchArt(@NonNull MediaDescriptionCompat description) {
    Bitmap iconBitmap = description.getIconBitmap();
    Uri iconUri = description.getIconUri();
    Bitmap art = null;
    if (iconBitmap != null) {
        art = iconBitmap;
    } else if (iconUri != null) {
        try {
            art = Glide.with(getContext().getApplicationContext()).load(iconUri.toString()).asBitmap().diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY).into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
        } catch (InterruptedException | ExecutionException e) {
            Log.e(TAG, "Image art load failed", e);
        }
    }
    int backgroundColor = 0;
    if (art != null && art.getWidth() * 9 < art.getHeight() * 16) {
        // Portrait art requires dominant color as background color.
        Palette palette = new Palette.Builder(art).maximumColorCount(1).generate();
        backgroundColor = palette.getSwatches().isEmpty() ? 0 : palette.getSwatches().get(0).getRgb();
    }
    return new Pair<>(art, backgroundColor);
}
Also used : Palette(android.support.v7.graphics.Palette) Bitmap(android.graphics.Bitmap) Uri(android.net.Uri) Pair(android.support.v4.util.Pair)

Example 2 with MediaDescriptionCompat

use of android.support.v4.media.MediaDescriptionCompat in project AntennaPod by AntennaPod.

the class CustomMRControllerDialog method updateViews.

private void updateViews() {
    if (!viewsCreated || token == null || mediaController == null) {
        rootView.setVisibility(View.GONE);
        return;
    }
    MediaMetadataCompat metadata = mediaController.getMetadata();
    MediaDescriptionCompat description = metadata == null ? null : metadata.getDescription();
    if (description == null) {
        rootView.setVisibility(View.GONE);
        return;
    }
    PlaybackStateCompat state = mediaController.getPlaybackState();
    MediaRouter.RouteInfo route = MediaRouter.getInstance(getContext()).getSelectedRoute();
    CharSequence title = description.getTitle();
    boolean hasTitle = !TextUtils.isEmpty(title);
    CharSequence subtitle = description.getSubtitle();
    boolean hasSubtitle = !TextUtils.isEmpty(subtitle);
    boolean showTitle = false;
    boolean showSubtitle = false;
    if (route.getPresentationDisplayId() != MediaRouter.RouteInfo.PRESENTATION_DISPLAY_ID_NONE) {
        // The user is currently casting screen.
        titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_casting_screen);
        showTitle = true;
    } else if (state == null || state.getState() == PlaybackStateCompat.STATE_NONE) {
        // (Only exception is bluetooth where we don't show anything.)
        if (!route.isDeviceTypeBluetooth()) {
            titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_no_media_selected);
            showTitle = true;
        }
    } else if (!hasTitle && !hasSubtitle) {
        titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_no_info_available);
        showTitle = true;
    } else {
        if (hasTitle) {
            titleView.setText(title);
            showTitle = true;
        }
        if (hasSubtitle) {
            subtitleView.setText(subtitle);
            showSubtitle = true;
        }
    }
    if (showSubtitle) {
        titleView.setSingleLine();
    } else {
        titleView.setMaxLines(2);
    }
    titleView.setVisibility(showTitle ? View.VISIBLE : View.GONE);
    subtitleView.setVisibility(showSubtitle ? View.VISIBLE : View.GONE);
    updateState();
    if (rootView.getVisibility() != View.VISIBLE) {
        artView.setVisibility(View.GONE);
        rootView.setVisibility(View.VISIBLE);
    }
    if (fetchArtSubscription != null) {
        fetchArtSubscription.unsubscribe();
    }
    fetchArtSubscription = Observable.fromCallable(() -> fetchArt(description)).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
        fetchArtSubscription = null;
        if (artView == null) {
            return;
        }
        if (result.first != null) {
            if (!((Boolean) artView.getTag())) {
                artView.setBackgroundColor(result.second);
            }
            artView.setImageBitmap(result.first);
            artView.setVisibility(View.VISIBLE);
        } else {
            artView.setVisibility(View.GONE);
        }
    }, error -> Log.e(TAG, Log.getStackTraceString(error)));
}
Also used : Context(android.content.Context) ImageButton(android.widget.ImageButton) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) Palette(android.support.v7.graphics.Palette) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat) Uri(android.net.Uri) FrameLayout(android.widget.FrameLayout) ImageView(android.widget.ImageView) AccessibilityEventCompat(android.support.v4.view.accessibility.AccessibilityEventCompat) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) RemoteException(android.os.RemoteException) PendingIntent(android.app.PendingIntent) NonNull(android.support.annotation.NonNull) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat) MarginLayoutParamsCompat(android.support.v4.view.MarginLayoutParamsCompat) ApGlideSettings(de.danoeh.antennapod.core.glide.ApGlideSettings) Drawable(android.graphics.drawable.Drawable) Observable(rx.Observable) AccessibilityManager(android.view.accessibility.AccessibilityManager) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Schedulers(rx.schedulers.Schedulers) View(android.view.View) AccessibilityEvent(android.view.accessibility.AccessibilityEvent) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) Log(android.util.Log) R(de.danoeh.antennapod.R) Target(com.bumptech.glide.request.target.Target) TextUtils(android.text.TextUtils) ViewGroup(android.view.ViewGroup) ExecutionException(java.util.concurrent.ExecutionException) MediaRouter(android.support.v7.media.MediaRouter) TextView(android.widget.TextView) Glide(com.bumptech.glide.Glide) MediaRouteControllerDialog(android.support.v7.app.MediaRouteControllerDialog) TypedValue(android.util.TypedValue) Bitmap(android.graphics.Bitmap) Pair(android.support.v4.util.Pair) Configuration(android.content.res.Configuration) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) Subscription(rx.Subscription) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaRouter(android.support.v7.media.MediaRouter) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 3 with MediaDescriptionCompat

use of android.support.v4.media.MediaDescriptionCompat in project android-UniversalMusicPlayer by googlesamples.

the class CardPresenter method onBindViewHolder.

@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
    MediaDescriptionCompat description;
    final CardViewHolder cardViewHolder = (CardViewHolder) viewHolder;
    // Determine description and playing state of item based on instance type
    cardViewHolder.setState(MediaItemViewHolder.STATE_NONE);
    if (item instanceof MediaBrowserCompat.MediaItem) {
        MediaBrowserCompat.MediaItem mediaItem = (MediaBrowserCompat.MediaItem) item;
        LogHelper.d(TAG, "onBindViewHolder MediaItem: ", mediaItem.toString());
        description = mediaItem.getDescription();
        cardViewHolder.setState(MediaItemViewHolder.getMediaItemState(mContext, mediaItem));
    } else if (item instanceof MediaSessionCompat.QueueItem) {
        MediaSessionCompat.QueueItem queueItem = (MediaSessionCompat.QueueItem) item;
        LogHelper.d(TAG, "onBindViewHolder QueueItem: ", queueItem.toString());
        description = queueItem.getDescription();
        if (QueueHelper.isQueueItemPlaying(mContext, queueItem)) {
            cardViewHolder.setState(MediaItemViewHolder.getStateFromController(mContext));
        }
    } else {
        throw new IllegalArgumentException("Object must be MediaItem or QueueItem, not " + item.getClass().getSimpleName());
    }
    cardViewHolder.setupCardView(mContext, description);
}
Also used : MediaBrowserCompat(android.support.v4.media.MediaBrowserCompat) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 4 with MediaDescriptionCompat

use of android.support.v4.media.MediaDescriptionCompat in project android-UniversalMusicPlayer by googlesamples.

the class MediaItemViewHolder method setupListView.

// Returns a view for use in media item list.
static View setupListView(Activity activity, View convertView, ViewGroup parent, MediaBrowserCompat.MediaItem item) {
    if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
        initializeColorStateLists(activity);
    }
    MediaItemViewHolder holder;
    Integer cachedState = STATE_INVALID;
    if (convertView == null) {
        convertView = LayoutInflater.from(activity).inflate(R.layout.media_list_item, parent, false);
        holder = new MediaItemViewHolder();
        holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
        holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
        holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (MediaItemViewHolder) convertView.getTag();
        cachedState = (Integer) convertView.getTag(R.id.tag_mediaitem_state_cache);
    }
    MediaDescriptionCompat description = item.getDescription();
    holder.mTitleView.setText(description.getTitle());
    holder.mDescriptionView.setText(description.getSubtitle());
    // If the state of convertView is different, we need to adapt the view to the
    // new state.
    int state = getMediaItemState(activity, item);
    if (cachedState == null || cachedState != state) {
        Drawable drawable = getDrawableByState(activity, state);
        if (drawable != null) {
            holder.mImageView.setImageDrawable(drawable);
            holder.mImageView.setVisibility(View.VISIBLE);
        } else {
            holder.mImageView.setVisibility(View.GONE);
        }
        convertView.setTag(R.id.tag_mediaitem_state_cache, state);
    }
    return convertView;
}
Also used : AnimationDrawable(android.graphics.drawable.AnimationDrawable) Drawable(android.graphics.drawable.Drawable) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 5 with MediaDescriptionCompat

use of android.support.v4.media.MediaDescriptionCompat in project AndroidAudioExample by SergeyVinyar.

the class MediaStyleHelper method from.

/**
 * Build a notification using the information from the given media session. Makes heavy use
 * of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information.
 *
 * @param context      Context used to construct the notification.
 * @param mediaSession Media session to get information.
 * @return A pre-built notification with information from the given media session.
 */
static NotificationCompat.Builder from(Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    MediaDescriptionCompat description = mediaMetadata.getDescription();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(description.getTitle()).setContentText(description.getSubtitle()).setSubText(description.getDescription()).setLargeIcon(description.getIconBitmap()).setContentIntent(controller.getSessionActivity()).setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP)).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    return builder;
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) NotificationCompat(android.support.v7.app.NotificationCompat) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Aggregations

MediaDescriptionCompat (android.support.v4.media.MediaDescriptionCompat)13 Bitmap (android.graphics.Bitmap)4 Uri (android.net.Uri)4 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)3 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 Drawable (android.graphics.drawable.Drawable)2 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)2 Pair (android.support.v4.util.Pair)2 Palette (android.support.v7.graphics.Palette)2 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 Configuration (android.content.res.Configuration)1 AnimationDrawable (android.graphics.drawable.AnimationDrawable)1 Bundle (android.os.Bundle)1 RemoteException (android.os.RemoteException)1 NonNull (android.support.annotation.NonNull)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 MediaBrowserCompat (android.support.v4.media.MediaBrowserCompat)1 MediaStyle (android.support.v4.media.app.NotificationCompat.MediaStyle)1 QueueItem (android.support.v4.media.session.MediaSessionCompat.QueueItem)1