Java Code Examples for android.support.v4.media.session.PlaybackStateCompat#getState()

The following examples show how to use android.support.v4.media.session.PlaybackStateCompat#getState() . 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: 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 2
Source File: PlaybackControlsFragment.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    PlaybackStateCompat stateObj = mMediaControllerProvider.getSupportMediaController().getPlaybackState();
    final int state = stateObj == null ?
            PlaybackStateCompat.STATE_NONE : stateObj.getState();
    LogUtils.d(TAG, "Button pressed, in state " + state);
    switch (v.getId()) {
        case R.id.play_pause:
            LogUtils.d(TAG, "Play button pressed, in state " + state);
            if (state == PlaybackStateCompat.STATE_PAUSED ||
                    state == PlaybackStateCompat.STATE_STOPPED ||
                    state == PlaybackStateCompat.STATE_NONE) {
                playMedia();
            } else if (state == PlaybackStateCompat.STATE_PLAYING ||
                    state == PlaybackStateCompat.STATE_BUFFERING ||
                    state == PlaybackStateCompat.STATE_CONNECTING) {
                pauseMedia();
            }
            break;
    }
}
 
Example 3
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 4
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 5
Source File: MediaNotificationManager.java    From klingar with Apache License 2.0 6 votes vote down vote up
private void setNotificationPlaybackState(NotificationCompat.Builder builder) {
  PlaybackStateCompat playbackState = musicController.getPlaybackState();
  if (playbackState == null || !started) {
    service.stopForeground(true);
    return;
  }
  if (playbackState.getState() == PlaybackStateCompat.STATE_PLAYING
      && playbackState.getPosition() >= 0) {
    builder.setWhen(System.currentTimeMillis() - playbackState.getPosition())
        .setShowWhen(true)
        .setUsesChronometer(true);
  } else {
    builder.setWhen(0)
        .setShowWhen(false)
        .setUsesChronometer(false);
  }
  // Make sure that the notification can be dismissed by the user when we are not playing:
  builder.setOngoing(playbackState.getState() == PlaybackStateCompat.STATE_PLAYING);
}
 
Example 6
Source File: NotificationHandler.java    From Bop with Apache License 2.0 5 votes vote down vote up
@Override
public void onPlaybackStateChanged(@NonNull PlaybackStateCompat state) {
    mPlaybackState = state;
    if (state.getState() == PlaybackStateCompat.STATE_STOPPED ||
            state.getState() == PlaybackStateCompat.STATE_NONE) {
        stopNotification();
    } else {
        Notification notification = createNotification();
        if (notification != null) {
            mNotificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
}
 
Example 7
Source File: PlaybackControlsFragment.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
private void onPlaybackStateChanged(PlaybackStateCompat state) {
    LogUtils.d(TAG, "onPlaybackStateChanged ", state);
    if (getActivity() == null) {
        LogUtils.w(TAG, "onPlaybackStateChanged called when getActivity null," +
                "this should not happen if the callback was properly unregistered. Ignoring.");
        return;
    }
    if (state == null) {
        return;
    }
    boolean enablePlay = false;
    switch (state.getState()) {
        case PlaybackStateCompat.STATE_PAUSED:
        case PlaybackStateCompat.STATE_STOPPED:
            enablePlay = true;
            break;
        case PlaybackStateCompat.STATE_ERROR:
            LogUtils.e(TAG, "error playbackstate: ", state.getErrorMessage());
            Toast.makeText(getActivity(), state.getErrorMessage(), Toast.LENGTH_LONG).show();
            break;
    }

    if (enablePlay) {
        mPlayPause.setImageDrawable(
                ActivityCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_36dp));
    } else {
        mPlayPause.setImageDrawable(
                ActivityCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_36dp));
    }

    MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController();
    String extraInfo = null;
    if (controller != null && controller.getExtras() != null) {
        String castName = controller.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            extraInfo = getResources().getString(R.string.casting_to_device, castName);
        }
    }
    setExtraInfo(extraInfo);
}
 
Example 8
Source File: RemoteControlCompat.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public static boolean isPlaying(PlaybackStateCompat state) {
    if (null == state) return false;
    switch (state.getState()) {
        case PlaybackStateCompat.STATE_PLAYING:
        case PlaybackStateCompat.STATE_FAST_FORWARDING:
        case PlaybackStateCompat.STATE_REWINDING:
        case PlaybackStateCompat.STATE_SKIPPING_TO_NEXT:
        case PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS:
            return true;
    }
    return false;
}
 
Example 9
Source File: PlaybackControlsFragment.java    From YouTube-In-Background with MIT License 5 votes vote down vote up
private void updatePlaybackState(PlaybackStateCompat state)
    {
        if (state == null) return;

//        LogHelper.e(TAG, "updatePlaybackState called ");
        lastPlaybackState = state;

        switch (state.getState()) {
            case STATE_PLAYING:
                playPause.setVisibility(VISIBLE);
                playPause.setImageDrawable(pauseDrawable);
                controls.setVisibility(VISIBLE);
                scheduleSeekbarUpdate();
                break;
            case STATE_PAUSED:
                controls.setVisibility(VISIBLE);
                playPause.setVisibility(VISIBLE);
                playPause.setImageDrawable(playDrawable);
                stopSeekbarUpdate();
                break;
            case STATE_NONE:
            case STATE_STOPPED:
                playPause.setVisibility(VISIBLE);
                playPause.setImageDrawable(playDrawable);
                stopSeekbarUpdate();
                break;
            case STATE_BUFFERING:
                playPause.setVisibility(INVISIBLE);
                stopSeekbarUpdate();
                break;
            default:
                LogHelper.d(TAG, "Unhandled state ", state.getState());
        }

        skipNext.setVisibility((state.getActions() & ACTION_SKIP_TO_NEXT) == 0 ? INVISIBLE : VISIBLE);
        skipPrev.setVisibility((state.getActions() & ACTION_SKIP_TO_PREVIOUS) == 0 ? INVISIBLE : VISIBLE);
    }
 
Example 10
Source File: MediaNotificationManager.java    From android-MediaBrowserService with Apache License 2.0 5 votes vote down vote up
public Notification getNotification(MediaMetadataCompat metadata,
                                    @NonNull PlaybackStateCompat state,
                                    MediaSessionCompat.Token token) {
    boolean isPlaying = state.getState() == PlaybackStateCompat.STATE_PLAYING;
    MediaDescriptionCompat description = metadata.getDescription();
    NotificationCompat.Builder builder =
            buildNotification(state, token, isPlaying, description);
    return builder.build();
}
 
Example 11
Source File: TrackNotification.java    From Melophile with Apache License 2.0 5 votes vote down vote up
public void updatePlaybackState(PlaybackStateCompat playbackState) {
  this.playbackState = playbackState;
  if (playbackState.getState() == PlaybackStateCompat.STATE_STOPPED ||
          playbackState.getState() == PlaybackStateCompat.STATE_NONE) {
    stopNotification();
  } else {
    updateNotification();
  }
}
 
Example 12
Source File: MediaNotificationManager.java    From LyricHere with Apache License 2.0 5 votes vote down vote up
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
    mPlaybackState = state;
    LogUtils.d(TAG, "Received new playback state", state);
    if (state != null && (state.getState() == PlaybackStateCompat.STATE_STOPPED ||
            state.getState() == PlaybackStateCompat.STATE_NONE)) {
        stopNotification();
    } else {
        Notification notification = createNotification();
        if (notification != null) {
            mNotificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
}
 
Example 13
Source File: MediaNotificationManager.java    From react-native-streaming-audio-player with MIT License 5 votes vote down vote up
@Override
public void onPlaybackStateChanged(@NonNull PlaybackStateCompat state) {
    mPlaybackState = state;
    if (state.getState() == PlaybackStateCompat.STATE_STOPPED ||
            state.getState() == PlaybackStateCompat.STATE_NONE) {
        stopNotification();
    } else {
        Notification notification = createNotification();
        if (notification != null) {
            mNotificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
}
 
Example 14
Source File: MainActivity.java    From blade-player with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state)
{
    if(state.getState() == PlaybackStateCompat.STATE_STOPPED) {hideCurrentPlay(); return;}

    if(musicPlayer != null)
        showCurrentPlay(musicPlayer.getCurrentSong(), musicPlayer.isPlaying());
}
 
Example 15
Source File: MediaNotificationManager.java    From react-native-audio-streaming-player with MIT License 5 votes vote down vote up
@Override
public void onPlaybackStateChanged(@NonNull PlaybackStateCompat state) {
    mPlaybackState = state;
    if (state.getState() == PlaybackStateCompat.STATE_STOPPED ||
            state.getState() == PlaybackStateCompat.STATE_NONE) {
        stopNotification();
    } else {
        Notification notification = createNotification();
        if (notification != null) {
            mNotificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
}
 
Example 16
Source File: PlayerView.java    From Bop with Apache License 2.0 5 votes vote down vote up
private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;
    }

    switch (state.getState()) {
        case PlaybackStateCompat.STATE_PLAYING:
            mFabView.setImageDrawable((ExtraUtils.getThemedIcon(getApplicationContext(), ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_pause))));
            break;
        case PlaybackStateCompat.STATE_PAUSED:
            mFabView.setImageDrawable((ExtraUtils.getThemedIcon(getApplicationContext(), ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_play))));
            break;
        case PlaybackStateCompat.STATE_NONE:
            break;
        case PlaybackStateCompat.STATE_STOPPED:
            finish();
            break;
        default:
        case PlaybackStateCompat.STATE_BUFFERING:
            break;
        case PlaybackStateCompat.STATE_CONNECTING:
            break;
        case PlaybackStateCompat.STATE_ERROR:
            break;
        case PlaybackStateCompat.STATE_FAST_FORWARDING:
            break;
        case PlaybackStateCompat.STATE_REWINDING:
            break;
        case PlaybackStateCompat.STATE_SKIPPING_TO_NEXT:
            break;
        case PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS:
            break;
        case PlaybackStateCompat.STATE_SKIPPING_TO_QUEUE_ITEM:
            break;
    }

}
 
Example 17
Source File: MediaNotificationManager.java    From carstream-android-auto with Apache License 2.0 5 votes vote down vote up
@Override
public void onPlaybackStateChanged(@NonNull PlaybackStateCompat state) {
    mPlaybackState = state;
    Log.d(TAG, "Received new playback state" + state);
    if (state.getState() == PlaybackStateCompat.STATE_STOPPED ||
            state.getState() == PlaybackStateCompat.STATE_NONE) {
        stopNotification();
    } else {
        Notification notification = createNotification();
        if (notification != null) {
            mNotificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
}
 
Example 18
Source File: MediaNotificationManager.java    From YouTube-In-Background with MIT License 4 votes vote down vote up
/**
 * This may look strange, but the documentation for [Service.startForeground]
 * notes that "calling this method does *not* put the service in the started
 * state itself, even though the name sounds like it."
 *
 * @param state current playback state
 */
private void updateNotification(PlaybackStateCompat state)
{
    int updatedState = state.getState();

    // Skip building a notification when state is "none" and metadata is null.
    Notification notification = skipBuildNotification(updatedState) ? buildNotification(sessionToken) : null;

    if (notification != null && !hasRegisterReceiver) {
        mediaController.registerCallback(callback);
        IntentFilter filter = new IntentFilter();
        filter.addAction(CUSTOM_ACTION_NEXT);
        filter.addAction(CUSTOM_ACTION_PAUSE);
        filter.addAction(CUSTOM_ACTION_PLAY);
        filter.addAction(CUSTOM_ACTION_PREV);
        filter.addAction(CUSTOM_ACTION_STOP);
        exoAudioService.registerReceiver(this, filter);
        hasRegisterReceiver = true;
    }

    switch (updatedState) {
        case STATE_BUFFERING:
        case STATE_PLAYING:
            if (notification != null) {
                notificationManager.notify(NOTIFICATION_ID, notification);
            }

            if (!isForegroundService) {
                Intent intent = new Intent(context, BackgroundExoAudioService.class);
                ContextCompat.startForegroundService(context, intent);
                exoAudioService.startForeground(NOTIFICATION_ID, notification);
                isForegroundService = true;
            }

            break;
        case PlaybackStateCompat.STATE_CONNECTING:
        case PlaybackStateCompat.STATE_ERROR:
        case PlaybackStateCompat.STATE_FAST_FORWARDING:
        case PlaybackStateCompat.STATE_NONE:
        case PlaybackStateCompat.STATE_PAUSED:
        case PlaybackStateCompat.STATE_REWINDING:
        case PlaybackStateCompat.STATE_SKIPPING_TO_NEXT:
        case PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS:
        case PlaybackStateCompat.STATE_SKIPPING_TO_QUEUE_ITEM:
        case PlaybackStateCompat.STATE_STOPPED:
            if (isForegroundService) {
                exoAudioService.stopForeground(false);
                isForegroundService = false;

                // If playback has ended, also stop the service.
                if (updatedState == PlaybackStateCompat.STATE_NONE) {
                    exoAudioService.stopSelf();
                }

                if (notification != null) {
                    notificationManager.notify(NOTIFICATION_ID, notification);
                } else {
                    removeNowPlayingNotification();
                }
            }

            break;
    }
}
 
Example 19
Source File: MainActivity.java    From android-MediaBrowserService with Apache License 2.0 4 votes vote down vote up
@Override
public void onPlaybackStateChanged(PlaybackStateCompat playbackState) {
    mIsPlaying = playbackState != null &&
            playbackState.getState() == PlaybackStateCompat.STATE_PLAYING;
    mMediaControlsImage.setPressed(mIsPlaying);
}
 
Example 20
Source File: FullScreenPlayerActivity.java    From LyricHere with Apache License 2.0 4 votes vote down vote up
private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;
    }
    mLastPlaybackState = state;
    if (mMediaController != null && mMediaController.getExtras() != null) {
        String castName = mMediaController.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        String line3Text = castName == null ? "" : getResources()
                .getString(R.string.casting_to_device, castName);
        mLine3.setText(line3Text);
    }

    switch (state.getState()) {
        case PlaybackStateCompat.STATE_PLAYING:
            mLoading.setVisibility(INVISIBLE);
            mPlayPause.setVisibility(VISIBLE);
            mPlayPause.setImageDrawable(mPauseDrawable);
            mControllers.setVisibility(VISIBLE);
            scheduleSeekbarUpdate();
            break;
        case PlaybackStateCompat.STATE_PAUSED:
            mControllers.setVisibility(VISIBLE);
            mLoading.setVisibility(INVISIBLE);
            mPlayPause.setVisibility(VISIBLE);
            mPlayPause.setImageDrawable(mPlayDrawable);
            stopSeekbarUpdate();
            break;
        case PlaybackStateCompat.STATE_NONE:
        case PlaybackStateCompat.STATE_STOPPED:
            mLoading.setVisibility(INVISIBLE);
            mPlayPause.setVisibility(VISIBLE);
            mPlayPause.setImageDrawable(mPlayDrawable);
            stopSeekbarUpdate();
            break;
        case PlaybackStateCompat.STATE_BUFFERING:
            mPlayPause.setVisibility(INVISIBLE);
            mLoading.setVisibility(VISIBLE);
            mLine3.setText(R.string.loading);
            stopSeekbarUpdate();
            break;
        default:
            LogUtils.d(TAG, "Unhandled state ", state.getState());
    }

    mSkipNext.setVisibility((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) == 0
            ? INVISIBLE : VISIBLE);
    mSkipPrev.setVisibility((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) == 0
            ? INVISIBLE : VISIBLE);
}