Example usage for android.support.v4.media.session MediaControllerCompat getPlaybackState

List of usage examples for android.support.v4.media.session MediaControllerCompat getPlaybackState

Introduction

In this page you can find the example usage for android.support.v4.media.session MediaControllerCompat getPlaybackState.

Prototype

public PlaybackStateCompat getPlaybackState() 

Source Link

Usage

From source file:com.example.android.mediabrowserservice.MediaNotificationHelper.java

public static Notification createNotification(Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mMetadata = controller.getMetadata();
    PlaybackStateCompat mPlaybackState = controller.getPlaybackState();

    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }//from w  w  w .  jav  a 2  s  .c om

    boolean isPlaying = mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING;
    NotificationCompat.Action action = isPlaying
            ? new NotificationCompat.Action(R.drawable.ic_pause_white_24dp,
                    context.getString(R.string.label_pause),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                            PlaybackStateCompat.ACTION_PAUSE))
            : new NotificationCompat.Action(R.drawable.ic_play_arrow_white_24dp,
                    context.getString(R.string.label_play), MediaButtonReceiver
                            .buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY));

    MediaDescriptionCompat description = mMetadata.getDescription();
    Bitmap art = description.getIconBitmap();
    if (art == null) {
        // use a placeholder art while the remote art is being downloaded.
        art = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_default_art);
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
    notificationBuilder.setStyle(new NotificationCompat.MediaStyle()
            // show only play/pause in compact view.
            .setShowActionsInCompactView(new int[] { 0 }).setMediaSession(mediaSession.getSessionToken()))
            .addAction(action).setSmallIcon(R.drawable.ic_notification).setShowWhen(false)
            .setContentIntent(controller.getSessionActivity()).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    return notificationBuilder.build();
}

From source file:cat.terrones.devops.radiofx.ui.MediaItemViewHolder.java

public static int getStateFromController(Context context) {
    MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
    PlaybackStateCompat pbState = controller.getPlaybackState();
    if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
        return MediaItemViewHolder.STATE_NONE;
    } else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        return MediaItemViewHolder.STATE_PLAYING;
    } else {//w w  w  .  j  ava  2  s.  c om
        return MediaItemViewHolder.STATE_PAUSED;
    }
}

From source file:com.classiqo.nativeandroid_32bitz.ui.MediaItemViewHolder.java

public static int getStateFromController(Context context) {
    MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
    PlaybackStateCompat pbState = controller.getPlaybackState();

    if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
        return MediaItemViewHolder.STATE_NONE;
    } else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        return MediaItemViewHolder.STATE_PLAYING;
    } else {/*from   ww  w . ja v a  2  s  .com*/
        return MediaItemViewHolder.STATE_PAUSED;
    }
}

From source file:rocks.stalin.android.app.ui.MediaItemViewHolder.java

public static int getStateFromController(Context context) {
    MediaControllerCompat controller = MediaControllerCompat.getMediaController((FragmentActivity) context);
    PlaybackStateCompat pbState = controller.getPlaybackState();
    if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
        return MediaItemViewHolder.STATE_NONE;
    } else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        return MediaItemViewHolder.STATE_PLAYING;
    } else {/*w  w w.  ja v a  2s . c om*/
        return MediaItemViewHolder.STATE_PAUSED;
    }
}

From source file:com.murati.oszk.audiobook.utils.QueueHelper.java

/**
 * Determine if queue item matches the currently playing queue item
 *
 * @param context for retrieving the {@link MediaControllerCompat}
 * @param queueItem to compare to currently playing {@link MediaSessionCompat.QueueItem}
 * @return boolean indicating whether queue item matches currently playing queue item
 *///from w w w.  j ava 2  s  .  com
public static boolean isQueueItemPlaying(Activity context, MediaSessionCompat.QueueItem queueItem) {
    // Queue item is considered to be playing or paused based on both the controller's
    // current media id and the controller's active queue item id
    MediaControllerCompat controller = MediaControllerCompat.getMediaController(context);
    if (controller != null && controller.getPlaybackState() != null) {
        long currentPlayingQueueId = controller.getPlaybackState().getActiveQueueItemId();
        String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
        String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
        if (queueItem.getQueueId() == currentPlayingQueueId && currentPlayingMediaId != null
                && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
            return true;
        }
    }
    return false;
}

From source file:com.radiofarda.istgah.utils.QueueHelper.java

/**
 * Determine if queue item matches the currently playing queue item
 *
 * @param context   for retrieving the {@link MediaControllerCompat}
 * @param queueItem to compare to currently playing {@link MediaSessionCompat.QueueItem}
 * @return boolean indicating whether queue item matches currently playing queue item
 *//*  w w w .  j  a v a2 s.  com*/
public static boolean isQueueItemPlaying(Context context, MediaSessionCompat.QueueItem queueItem) {
    // Queue item is considered to be playing or paused based on both the controller's
    // current media id and the controller's active queue item id
    MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
    if (controller != null && controller.getPlaybackState() != null) {
        long currentPlayingQueueId = controller.getPlaybackState().getActiveQueueItemId();
        String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
        String itemMusicId = queueItem.getDescription().getMediaId();
        if (queueItem.getQueueId() == currentPlayingQueueId && currentPlayingMediaId != null
                && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
            return true;
        }
    }
    return false;
}

From source file:com.classiqo.nativeandroid_32bitz.utils.QueueHelper.java

public static boolean isQueueItemPlaying(Context context, MediaSessionCompat.QueueItem queueItem) {
    MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
    if (controller != null && controller.getPlaybackState() != null) {
        long currentPlayingQueueId = controller.getPlaybackState().getActiveQueueItemId();
        String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
        String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());

        if (queueItem.getQueueId() == currentPlayingQueueId && currentPlayingMediaId != null
                && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
            return true;
        }/*from  w  w w . j  a v a  2 s . c  o m*/
    }

    return false;
}

From source file:com.example.android.uamp.utils.QueueHelper.java

/**
 * Determine if queue item matches the currently playing queue item
 *
 * @param context for retrieving the {@link MediaControllerCompat}
 * @param queueItem to compare to currently playing {@link MediaSessionCompat.QueueItem}
 * @return boolean indicating whether queue item matches currently playing queue item
 *///from w  w  w .j  a  v  a  2  s.  c  o  m
public static boolean isQueueItemPlaying(Context context, MediaSessionCompat.QueueItem queueItem) {
    // Queue item is considered to be playing or paused based on both the controller's
    // current media id and the controller's active queue item id
    MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
    if (controller != null && controller.getPlaybackState() != null) {
        long currentPlayingQueueId = controller.getPlaybackState().getActiveQueueItemId();
        String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
        String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
        if (queueItem.getQueueId() == currentPlayingQueueId && currentPlayingMediaId != null
                && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
            return true;
        }
    }
    return false;
}

From source file:rocks.stalin.android.app.utils.QueueHelper.java

/**
 * Determine if queue item matches the currently playing queue item
 *
 * @param context for retrieving the {@link MediaControllerCompat}
 * @param queueItem to compare to currently playing {@link MediaSessionCompat.QueueItem}
 * @return boolean indicating whether queue item matches currently playing queue item
 *///www  . j a v a2 s.c o  m
public static boolean isQueueItemPlaying(Context context, MediaSessionCompat.QueueItem queueItem) {
    // Queue item is considered to be playing or paused based on both the controller's
    // current media id and the controller's active queue item id
    MediaControllerCompat controller = MediaControllerCompat.getMediaController((FragmentActivity) context);
    if (controller != null && controller.getPlaybackState() != null) {
        long currentPlayingQueueId = controller.getPlaybackState().getActiveQueueItemId();
        String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
        String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
        if (queueItem.getQueueId() == currentPlayingQueueId && currentPlayingMediaId != null
                && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
            return true;
        }
    }
    return false;
}

From source file:com.murati.oszk.audiobook.ui.MediaItemViewHolder.java

public static int getStateFromController(Activity context) {
    MediaControllerCompat controller = MediaControllerCompat.getMediaController(context);
    PlaybackStateCompat pbState = controller.getPlaybackState();
    if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
        return MediaItemViewHolder.STATE_NONE;
    } else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        return MediaItemViewHolder.STATE_PLAYING;
    } else {/*from  w w w  .  j  av a  2 s.c o  m*/
        return MediaItemViewHolder.STATE_PAUSED;
    }
}