mSession.setPlaybackState(new PlaybackState.Builder() .setState(playState, position(), 1.0f) .setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) .build()); .build()); mSession.setPlaybackState(new PlaybackState.Builder() .setState(playState, position(), 1.0f) .setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) .build());
session.setPlaybackState(new PlaybackState.Builder() .setState(playbackState, service.getPosition(), 1.0f) .setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_STOP | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) .build()); mMediaSession.setActive(true);
PlaybackState state = new PlaybackState.Builder() .setActions(PlaybackState.ACTION_PLAY_PAUSE) .setState(PlaybackState.STATE_PLAYING, 0, 0, 0)
PlaybackState state = new PlaybackState.Builder() .setActions(PlaybackState.ACTION_PLAY) .setState(PlaybackState.STATE_PLAYING, position, 1.0f, SystemClock.elapsedRealtime())
/** * Update the current media player state, optionally showing an error message. * * @param error if not null, error message to present to the user. */ private void updatePlaybackState(String error) { long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN; int state = PlaybackState.STATE_PAUSED; if(PlaybackService.hasInstance()) { if (PlaybackService.get(this).isPlaying()) { state = PlaybackState.STATE_PLAYING; } position = PlaybackService.get(this).getPosition(); } PlaybackState.Builder stateBuilder = new PlaybackState.Builder() .setActions(getAvailableActions()); setCustomAction(stateBuilder); // If there is an error message, send it to the playback state: if (error != null) { // Error states are really only supposed to be used for errors that cause playback to // stop unexpectedly and persist until the user takes action to fix it. stateBuilder.setErrorMessage(error); state = PlaybackState.STATE_ERROR; } stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime()); mSession.setPlaybackState(stateBuilder.build()); } // 'DriveSafe' icons need to meet contrast requirement, and as such are usually
MediaSession.Callback callback = new MediaSession.Callback() { @Override public void onPlay() { // Handle the play button } }; MediaSession mediaSession = new MediaSession(context, TAG); // Debugging tag, any string mediaSession.setFlags( MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); mediaSession.setCallback(callback); // Set up what actions you support and the state of your player mediaSession.setState( new PlaybackState.Builder() .setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY_PAUSE); .setState(PlaybackState.STATE_PLAYING, 0, // playback position in milliseconds 1.0); // playback speed // Call this when you start playback after receiving audio focus mediaSession.setActive(true);
public static void updateMediaSession(boolean updateMetadata, MediaMetadata.Builder mediaBuilder, final MediaSession mediaSession, final int playbackState, final long position, final float speed) { if (mediaSession.isActive()) { if (updateMetadata) { mediaSession.setMetadata(mediaBuilder.build()); } PlaybackState.Builder stateBuilder = new PlaybackState.Builder(); stateBuilder.setState(playbackState, position, speed); mediaSession.setPlaybackState(stateBuilder.build()); } } }
private void setCustomAction(PlaybackState.Builder stateBuilder) { if(PlaybackService.hasInstance()) { Bundle customActionExtras = new Bundle(); final int finishMode = PlaybackService.finishAction(PlaybackService.get(this).getState()); final int shuffleMode = PlaybackService.shuffleMode(PlaybackService.get(this).getState()); stateBuilder.addCustomAction(new PlaybackState.CustomAction.Builder( CUSTOM_ACTION_REPEAT, getString(R.string.cycle_repeat_mode), FINISH_ICONS[finishMode]) .setExtras(customActionExtras) .build()); stateBuilder.addCustomAction(new PlaybackState.CustomAction.Builder( CUSTOM_ACTION_SHUFFLE, getString(R.string.cycle_shuffle_mode), SHUFFLE_ICONS[shuffleMode]) .setExtras(customActionExtras) .build()); } }
mediaSession.setMetadata(builder.build()); PlaybackState state = new PlaybackState.Builder() .setActions( PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState .ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_REWIND | PlaybackState.ACTION_FAST_FORWARD) .setState(PlaybackState.STATE_NONE, 0, 1.0f) .build(); mediaSession.setPlaybackState(state);
@Override public void closePlayer() { stopVideo(); PlaybackState.Builder stateBuilder = new PlaybackState.Builder(); stateBuilder.setState(PlaybackState.STATE_PAUSED, 12, 1); mMediaSession.setPlaybackState(stateBuilder.build()); mMediaSession.setActive(false); Log.i(TAG, "closePlayer : " + mMediaSession.isActive()); }
PlaybackState state = new PlaybackState.Builder() .setActions( PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackState.STATE_PLAYING, position, speed, SystemClock.elapsedRealtime()) .build(); mSession.setPlaybackState(state);
private void updatePlaybackState(int position) { PlaybackState.Builder stateBuilder = new PlaybackState.Builder() .setActions(getAvailableActions()); int state = PlaybackState.STATE_PLAYING; if (mPlaybackState == LeanbackPlaybackState.PAUSED) { state = PlaybackState.STATE_PAUSED; } stateBuilder.setState(state, position, 1.0f); mSession.setPlaybackState(stateBuilder.build()); }
public static Object newInstance(int state, long position, long bufferedPosition, float speed, long actions, CharSequence errorMessage, long updateTime) { PlaybackState.Builder stateObj = new PlaybackState.Builder(); stateObj.setState(state, position, speed, updateTime); stateObj.setBufferedPosition(bufferedPosition); stateObj.setActions(actions); stateObj.setErrorMessage(errorMessage); return stateObj.build(); } }
PlaybackState state = new PlaybackState.Builder() .setActions(PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_STOP) .setState(PlaybackStateCompat.STATE_PLAYING, 0, 1, SystemClock.elapsedRealtime()) .build(); mSession.setPlaybackState(state);
PlaybackState state = new PlaybackState.Builder() .setActions( PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) .setState(PlaybackState.STATE_PLAYING, position, speed, SystemClock.elapsedRealtime()) .build(); mSession.setPlaybackState(state);