PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder(); if (player == null) { builder.setActions(buildPlaybackActions()).setState(PlaybackStateCompat.STATE_NONE, 0, 0, 0); mediaSession.setPlaybackState(builder.build()); return; extras.putFloat(EXTRAS_PITCH, player.getPlaybackParameters().pitch); builder .setActions(buildPlaybackActions()) .setActiveQueueItemId(activeQueueItemId) .setBufferedPosition(player.getBufferedPosition())
mSession.setPlaybackState(new PlaybackStateCompat.Builder() .setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build()); .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build());
private void createMediaSession() { ComponentName receiver = new ComponentName(context.getPackageName(), RemoteReceiver.class.getName()); mediaSession = new MediaSessionCompat(context, "MusicService", receiver, null); mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); mediaSession.setPlaybackState( new PlaybackStateCompat.Builder() .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0) .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE) .build() ); AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.requestAudioFocus(focusChange -> {}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); mediaSession.setActive(true); }
mSession.setPlaybackState(new PlaybackStateCompat.Builder() .setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build()); .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build());
if (playbackState == PlaybackService.PLAYSTATE.PLAYING) { mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f) .setActions(PlaybackStateCompat.ACTION_SKIP_TO_NEXT + PlaybackStateCompat.ACTION_PAUSE + PlaybackStateCompat.ACTION_PLAY + PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS + PlaybackStateCompat.ACTION_STOP + PlaybackStateCompat.ACTION_SEEK_TO).build()); } else { mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder(). setState(PlaybackStateCompat.STATE_PAUSED, 0, 1.0f).setActions(PlaybackStateCompat.ACTION_SKIP_TO_NEXT + PlaybackStateCompat.ACTION_PAUSE + PlaybackStateCompat.ACTION_PLAY + PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS + PlaybackStateCompat.ACTION_STOP +
mSession.setPlaybackState(new PlaybackStateCompat.Builder() .setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build()); .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) .build());
if (isOnlyUpdateActions && stateBuilder != null) { stateBuilder.setActions(getAvailableActions()); mServiceCallback.onPlaybackStateUpdated(stateBuilder.build(), null); }else { .setActions(getAvailableActions());
int playState = mIsPreparing ? PlaybackStateCompat.STATE_BUFFERING : mPlayState; PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder() .setActions(actions) .setState(playState, getPlaybackPosition(), 1f, SystemClock.elapsedRealtime()) .setExtras(extras);
private void updateMediaSession() { if (!mMediaNotificationInfo.supportsPlayPause()) return; if (mMediaSession == null) mMediaSession = createMediaSession(); try { // Tell the MediaRouter about the session, so that Chrome can control the volume // on the remote cast device (if any). // Pre-MR1 versions of JB do not have the complete MediaRouter APIs, // so getting the MediaRouter instance will throw an exception. MediaRouter.getInstance(mContext).setMediaSessionCompat(mMediaSession); } catch (NoSuchMethodError e) { // Do nothing. Chrome can't be casting without a MediaRouter, so there is nothing // to do here. } mMediaSession.setMetadata(createMetadata()); PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder().setActions( PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE); if (mMediaNotificationInfo.isPaused) { playbackStateBuilder.setState(PlaybackStateCompat.STATE_PAUSED, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f); } else { // If notification only supports stop, still pretend playbackStateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f); } mMediaSession.setPlaybackState(playbackStateBuilder.build()); }
private void setMediaSessionPlaybackState() { if (mediaSession == null) { return; } long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN; if (textInterface != null) { position = textInterface.getTime(); } PlaybackStateCompat playbackState = new PlaybackStateCompat.Builder() .setActions( (this.state == State.PAUSED ? PlaybackStateCompat.ACTION_PLAY : PlaybackStateCompat.ACTION_PAUSE) | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_REWIND | PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_STOP ) .setState(this.state.playbackState, position, speed, SystemClock.elapsedRealtime()) .build(); mediaSession.setPlaybackState(playbackState); }
private void setUpAudioPlayerRemote() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName()); Intent audioIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); audioIntent.setComponent(audioReceiver); PendingIntent audioPendingIntent = PendingIntent.getBroadcast(this, 0, audioIntent, 0); mediaSession = new MediaSessionCompat(this, "PlayerService", audioReceiver, audioPendingIntent); mediaSession.setCallback(new MediaSessionCallback()); mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); mediaSession.setMediaButtonReceiver(audioPendingIntent); setSessionToken(mediaSession.getSessionToken()); try { mMediaNotificationManager = new MediaNotificationManager(this); } catch (RemoteException e) { throw new IllegalStateException("Could not create a MediaNotificationManager", e); } mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() .setState(PlaybackStateCompat.STATE_NONE, 0, 0) .setActions(getAvailableActions()) .build()); if (audioManager != null) { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
private void setNewState(@PlaybackStateCompat.State int newPlayerState) { mState = newPlayerState; // Whether playback goes to completion, or whether it is stopped, the // mCurrentMediaPlayedToCompletion is set to true. if (mState == PlaybackStateCompat.STATE_STOPPED) { mCurrentMediaPlayedToCompletion = true; } // Work around for MediaPlayer.getCurrentPosition() when it changes while not playing. final long reportPosition; if (mSeekWhileNotPlaying >= 0) { reportPosition = mSeekWhileNotPlaying; if (mState == PlaybackStateCompat.STATE_PLAYING) { mSeekWhileNotPlaying = -1; } } else { reportPosition = mMediaPlayer == null ? 0 : mMediaPlayer.getCurrentPosition(); } final PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder(); stateBuilder.setActions(getAvailableActions()); stateBuilder.setState(mState, reportPosition, 1.0f, SystemClock.elapsedRealtime()); mPlaybackInfoListener.onPlaybackStateChange(stateBuilder.build()); }
@Override public void reportPlaybackStateChanged( @NonNull final PlaybackState state, @Nullable final CharSequence errorMessage) { @PlaybackStateCompat.State final int playbackState = toPlaybackStateCompat(state); final boolean isPlaying = playbackState == PlaybackStateCompat.STATE_PLAYING; final PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder() .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_SEEK_TO | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH) .setState(playbackState, 0, isPlaying ? 1 : 0); if (errorMessage != null) { builder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_APP_ERROR, errorMessage); } mMediaSession.setPlaybackState(builder.build()); }
private PlaybackStateCompat createSessionPlaybackState() { long skipActions; if (isCarUiMode()) { skipActions = PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS; } else { skipActions = 0; } if (mStation == null || mStation.getPlaybackState() == PLAYBACK_STATE_STOPPED) { // define action for playback state to be used in media session callback return new PlaybackStateCompat.Builder() .setState(PlaybackStateCompat.STATE_STOPPED, 0, 0) .setActions(PlaybackStateCompat.ACTION_PLAY | skipActions) .build(); } else { // define action for playback state to be used in media session callback - car mode version return new PlaybackStateCompat.Builder() .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0) .setActions(PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID | skipActions) .build(); } }
private void updatePlaybackState() { playbackStateBuilder // Available actions .setActions( PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SEEK_TO) // Current playback state .setState( mediaPlayer.isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED, 0, // Track position in ms 1.0f); // Playback speed mMediaSession.setPlaybackState(playbackStateBuilder.build()); } }
private void updatePlaybackState( @PlaybackStateCompat.State int state, long position, MediaDescriptionCompat description) { PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder() .setActions(AVAILABLE_MEDIA_ACTIONS) .setActiveQueueItemId(Long.parseLong(description.getMediaId())) .setState(state, position, 1.0f); mSession.setPlaybackState(builder.build()); } }
private void updatePlaybackState(@NonNull PlayerState playerState, @NonNull ContentState contentState) { int position = playerState.positionInMs; //noinspection WrongConstant PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder() .setActions(getAvailableActions(playerState, contentState)); //noinspection WrongConstant stateBuilder.setState(getState(playerState), position, 1.0f); mNowPlayingSession.setPlaybackState(stateBuilder.build()); }
private void setMediaPlaybackState(int state) { PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder(); playbackStateBuilder.setActions(getAvailableActions()); playbackStateBuilder.setState(state, audioPlayer.getCurrentPosition(), 1.0f, SystemClock.elapsedRealtime()); mediaSession.setPlaybackState(playbackStateBuilder.build()); }
public void updatePlaybackState() { int state = (mPlayService.isPlaying() || mPlayService.isPreparing()) ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; mMediaSession.setPlaybackState( new PlaybackStateCompat.Builder() .setActions(MEDIA_SESSION_ACTIONS) .setState(state, mPlayService.getCurrentPosition(), 1) .build()); }
private void updatePlaybackState( @PlaybackStateCompat.State int state, long playbackActions, int position, int mediaId) { PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder() .setActions(playbackActions) .setActiveQueueItemId(mediaId) .setState(state, position, 1.0f); mSession.setPlaybackState(builder.build()); }