Java Code Examples for android.support.v4.media.session.MediaControllerCompat#getPlaybackState()

The following examples show how to use android.support.v4.media.session.MediaControllerCompat#getPlaybackState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TrackFragment.java    From Melophile with Apache License 2.0 6 votes vote down vote up
@OnClick(R.id.play_pause)
public void playPause() {
  lastState = null;
  MediaControllerCompat controllerCompat = MediaControllerCompat.getMediaController(getActivity());
  PlaybackStateCompat stateCompat = controllerCompat.getPlaybackState();
  if (stateCompat != null) {
    MediaControllerCompat.TransportControls controls =
            controllerCompat.getTransportControls();
    switch (stateCompat.getState()) {
      case PlaybackStateCompat.STATE_PLAYING:
      case PlaybackStateCompat.STATE_BUFFERING:
        controls.pause();
        break;
      case PlaybackStateCompat.STATE_NONE:
      case PlaybackStateCompat.STATE_PAUSED:
      case PlaybackStateCompat.STATE_STOPPED:
        controls.play();
        break;
      default:
        Log.d(TAG, "State " + stateCompat.getState());
    }
  }
}
 
Example 2
Source File: PlaybackControlsFragment.java    From YouTube-In-Background with MIT License 6 votes vote down vote up
public void onConnected()
{
    MediaControllerCompat controller = MediaControllerCompat.getMediaController(activity);
    LogHelper.e(TAG, "onConnected, mediaController==null? ", controller == null);
    if (controller != null) {
        controller.registerCallback(mediaControllerCallback);
        onPlaybackStateChanged(controller.getPlaybackState());
        onMetadataChanged(controller.getMetadata());

        PlaybackStateCompat state = controller.getPlaybackState();
        updatePlaybackState(state);
        MediaMetadataCompat metadata = controller.getMetadata();
        if (metadata != null) {
            updateMediaDescription(metadata.getDescription());
            updateDuration(metadata);
        }
        updateProgress();
        if (state != null && (state.getState() == STATE_PLAYING || state.getState() == STATE_BUFFERING)) {
            scheduleSeekbarUpdate();
        }
    }
}
 
Example 3
Source File: MainActivity.java    From YouTube-In-Background with MIT License 6 votes vote down vote up
/**
 * Check if the MediaSession is active and in a "playback-able" state
 * (not NONE and not STOPPED).
 *
 * @return true if the MediaSession's state requires playback controls to be visible.
 */
protected boolean shouldShowControls()
{
    LogHelper.e(TAG, "shouldShowControls");
    MediaControllerCompat mediaController = MediaControllerCompat.getMediaController(this);
    if (mediaController == null ||
            mediaController.getMetadata() == null ||
            mediaController.getPlaybackState() == null) {
        return false;
    }
    switch (mediaController.getPlaybackState().getState()) {
        case STATE_ERROR:
        case STATE_NONE:
        case STATE_STOPPED:
            return false;
        default:
            return true;
    }
}
 
Example 4
Source File: MediaBrowserFragment.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowserCompat.MediaItem item = getItem(position);
    int itemState = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        itemState = MediaItemViewHolder.STATE_PLAYABLE;
        MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController();
        if (controller != null && controller.getMetadata() != null) {
            String currentPlaying = controller.getMetadata().getDescription().getMediaId();
            String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                    item.getDescription().getMediaId());
            if (currentPlaying != null && currentPlaying.equals(musicId)) {
                PlaybackStateCompat pbState = controller.getPlaybackState();
                if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
                    itemState = MediaItemViewHolder.STATE_NONE;
                } else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
                    itemState = MediaItemViewHolder.STATE_PLAYING;
                } else {
                    itemState = MediaItemViewHolder.STATE_PAUSED;
                }
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
            item.getDescription(), itemState);
}
 
Example 5
Source File: PlaybackControlBaseActivity.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the MediaSession is active and in a "playback-able" state
 * (not NONE and not STOPPED).
 *
 * @return true if the MediaSession's state requires playback controls to be visible.
 */
protected boolean shouldShowControls() {
    MediaControllerCompat mediaController = mMediaController;
    if (mediaController == null ||
            mediaController.getMetadata() == null ||
            mediaController.getPlaybackState() == null) {
        return false;
    }
    switch (mediaController.getPlaybackState().getState()) {
        case PlaybackState.STATE_ERROR:
        case PlaybackState.STATE_NONE:
        case PlaybackState.STATE_STOPPED:
            return false;
        default:
            return true;
    }
}
 
Example 6
Source File: FullScreenPlayerActivity.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
private void connectToSession(MediaSessionCompat.Token token) {
    try {
        mMediaController = new MediaControllerCompat(FullScreenPlayerActivity.this, token);
        if (mMediaController.getMetadata() == null) {
            finish();
            return;
        }

        mMediaController.registerCallback(mCallback);
        PlaybackStateCompat state = mMediaController.getPlaybackState();
        updatePlaybackState(state);
        MediaMetadataCompat metadata = mMediaController.getMetadata();
        if (metadata != null) {
            updateMediaDescription(metadata.getDescription());
            updateDuration(metadata);
        }
        updateProgress();
        if (state != null && (state.getState() == PlaybackStateCompat.STATE_PLAYING ||
                state.getState() == PlaybackStateCompat.STATE_BUFFERING)) {
            scheduleSeekbarUpdate();
        }

    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: MediaBrowserFragment.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
private void checkForUserVisibleErrors(boolean forceError) {
    boolean showError = forceError;
    // If offline, message is about the lack of connectivity:
    if (!NetworkHelper.isOnline(getActivity())) {
        mErrorMessage.setText(R.string.error_no_connection);
        showError = true;
    } else {
        // otherwise, if state is ERROR and metadata!=null, use playback state error message:
        MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController();
        if (controller != null
                && controller.getMetadata() != null
                && controller.getPlaybackState() != null
                && controller.getPlaybackState().getState() == PlaybackStateCompat.STATE_ERROR
                && controller.getPlaybackState().getErrorMessage() != null) {
            mErrorMessage.setText(controller.getPlaybackState().getErrorMessage());
            showError = true;
        } else if (forceError) {
            // Finally, if the caller requested to show error, show a generic message:
            mErrorMessage.setText(R.string.error_loading_media);
            showError = true;
        }
    }
    mErrorView.setVisibility(showError ? View.VISIBLE : View.GONE);
    LogUtils.d(TAG, "checkForUserVisibleErrors. forceError=", forceError,
            " showError=", showError,
            " isOnline=", NetworkHelper.isOnline(getActivity()));
}
 
Example 8
Source File: NotificationBuilder.java    From YouTube-In-Background with MIT License 4 votes vote down vote up
public Notification buildNotification(MediaSessionCompat.Token sessionToken) throws RemoteException
{
    if (shouldCreateNowRunningChannel()) {
        createNotificationChannel();
    }
    mediaController = new MediaControllerCompat(context, sessionToken);
    MediaMetadataCompat metadata = mediaController.getMetadata();
    mediaDescription = metadata.getDescription();
    playbackState = mediaController.getPlaybackState();

    builder = new NotificationCompat.Builder(context, CHANNEL_ID);

    // Only add actions for skip back, play/pause, skip forward, based on what's enabled.
    int playPauseIndex = 0;
    if (isSkipToPreviousEnabled()) {
        builder.addAction(skipToPreviousAction);
        playPauseIndex++;
    }

    if (isPlaying()) {
        builder.addAction(pauseAction);
    } else if (isPlayEnabled()) {
        builder.addAction(playAction);
    }

    if (isSkipToNextEnabled()) {
        builder.addAction(skipToNextAction);
    }

    MediaStyle mediaStyle = new MediaStyle()
            .setCancelButtonIntent(stopPendingIntent)
            .setMediaSession(sessionToken)
            .setShowActionsInCompactView(playPauseIndex)
            .setShowCancelButton(true);

    return builder.setContentIntent(mediaController.getSessionActivity())
            .setContentText(mediaDescription.getSubtitle())
            .setContentTitle(mediaDescription.getTitle())
            .setDeleteIntent(stopPendingIntent)
            .setLargeIcon(mediaDescription.getIconBitmap())
            .setOnlyAlertOnce(true)
            .setSmallIcon(R.drawable.ic_notification)
            .setStyle(mediaStyle)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .build();
}