Search in sources :

Example 16 with MediaControllerCompat

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

the class QueueHelper method isQueueItemPlaying.

/**
 * 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
 */
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;
}
Also used : MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 17 with MediaControllerCompat

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

the class MediaIDHelper method isMediaItemPlaying.

/**
     * Determine if media item is playing (matches the currently playing media item).
     *
     * @param context for retrieving the {@link MediaControllerCompat}
     * @param mediaItem to compare to currently playing {@link MediaBrowserCompat.MediaItem}
     * @return boolean indicating whether media item matches currently playing media item
     */
public static boolean isMediaItemPlaying(Context context, MediaBrowserCompat.MediaItem mediaItem) {
    // Media item is considered to be playing or paused based on the controller's current
    // media id
    MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
    if (controller != null && controller.getMetadata() != null) {
        String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
        String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(mediaItem.getDescription().getMediaId());
        if (currentPlayingMediaId != null && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
            return true;
        }
    }
    return false;
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 18 with MediaControllerCompat

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

the class QueueHelper method isQueueItemPlaying.

/**
     * 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
     */
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;
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 19 with MediaControllerCompat

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

the class MediaNotificationManager method updateSessionToken.

/**
 * Update the state based on a change on the session token. Called either when
 * we are running for the first time or when the media session owner has destroyed the session
 * (see {@link android.media.session.MediaController.Callback#onSessionDestroyed()})
 */
private void updateSessionToken() throws RemoteException {
    MediaSessionCompat.Token freshToken = mService.getSessionToken();
    if (mSessionToken == null && freshToken != null || mSessionToken != null && !mSessionToken.equals(freshToken)) {
        if (mController != null) {
            mController.unregisterCallback(mCb);
        }
        mSessionToken = freshToken;
        if (mSessionToken != null) {
            mController = new MediaControllerCompat(mService, mSessionToken);
            mTransportControls = mController.getTransportControls();
            if (mStarted) {
                mController.registerCallback(mCb);
            }
        }
    }
}
Also used : MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 20 with MediaControllerCompat

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

the class FullScreenPlayerActivity method connectToSession.

private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
    MediaControllerCompat mediaController = new MediaControllerCompat(FullScreenPlayerActivity.this, token);
    if (mediaController.getMetadata() == null) {
        finish();
        return;
    }
    MediaControllerCompat.setMediaController(FullScreenPlayerActivity.this, mediaController);
    mediaController.registerCallback(mCallback);
    PlaybackStateCompat state = mediaController.getPlaybackState();
    updatePlaybackState(state);
    MediaMetadataCompat metadata = mediaController.getMetadata();
    if (metadata != null) {
        updateMediaDescription(metadata.getDescription());
        updateDuration(metadata);
    }
    updateProgress();
    if (state != null && (state.getState() == PlaybackStateCompat.STATE_PLAYING || state.getState() == PlaybackStateCompat.STATE_BUFFERING)) {
        scheduleSeekbarUpdate();
    }
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Aggregations

MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)27 FragmentActivity (android.support.v4.app.FragmentActivity)3 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)3 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)3 Intent (android.content.Intent)2 ListRow (android.support.v17.leanback.widget.ListRow)2 ListRowPresenter (android.support.v17.leanback.widget.ListRowPresenter)2 MediaBrowserCompat (android.support.v4.media.MediaBrowserCompat)2 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)2 View (android.view.View)2 AbstractDetailsDescriptionPresenter (android.support.v17.leanback.widget.AbstractDetailsDescriptionPresenter)1 Action (android.support.v17.leanback.widget.Action)1 HeaderItem (android.support.v17.leanback.widget.HeaderItem)1 OnActionClickedListener (android.support.v17.leanback.widget.OnActionClickedListener)1 OnItemViewClickedListener (android.support.v17.leanback.widget.OnItemViewClickedListener)1 PlaybackControlsRow (android.support.v17.leanback.widget.PlaybackControlsRow)1 PlayPauseAction (android.support.v17.leanback.widget.PlaybackControlsRow.PlayPauseAction)1 SkipNextAction (android.support.v17.leanback.widget.PlaybackControlsRow.SkipNextAction)1 SkipPreviousAction (android.support.v17.leanback.widget.PlaybackControlsRow.SkipPreviousAction)1 PlaybackControlsRowPresenter (android.support.v17.leanback.widget.PlaybackControlsRowPresenter)1