PlaybackBannerControlGlue

public class PlaybackBannerControlGlue<T extends PlayerAdapter> extends PlaybackBaseControlGlue


A helper class for managing a PlaybackControlsRow being displayed in PlaybackGlueHost. It supports standard playback control actions play/pause and skip next/previous. This helper class is a glue layer that manages interaction between the leanback UI components PlaybackControlsRowPlaybackControlsRowPresenter and a functional PlayerAdapter which represents the underlying media player.

Apps must pass a PlayerAdapter in the constructor for a specific implementation e.g. a MediaPlayerAdapter.

The glue has two action bars: primary action bars and secondary action bars. Apps can provide additional actions by overriding onCreatePrimaryActions and / or onCreateSecondaryActions and respond to actions by overriding onActionClicked.

The subclass is responsible for implementing the "repeat mode" in onPlayCompleted.

Sample Code:
public class MyVideoFragment extends VideoFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PlaybackBannerControlGlueplayerGlue =
                new PlaybackBannerControlGlue(getActivity(),
                        new MediaPlayerAdapter(getActivity()));
        playerGlue.setHost(new VideoFragmentGlueHost(this));
        playerGlue.setSubtitle("Leanback artist");
        playerGlue.setTitle("Leanback team at work");
        String uriPath = "android.resource://com.example.android.leanback/raw/video";
        playerGlue.getPlayerAdapter().setDataSource(Uri.parse(uriPath));
        playerGlue.playWhenPrepared();
    }
}
Parameters
<T extends PlayerAdapter>

Type of PlayerAdapter passed in constructor.

Summary

Constants

static final int

The adapter key for the first custom control on the left side of the predefined primary controls.

static final int

The adapter key for the first custom control on the right side of the predefined primary controls.

static final int

The adapter key for the fast forward control.

static final int

The adapter key for the play/pause control.

static final int

The adapter key for the rewind control.

static final int

The adapter key for the skip to next control.

static final int

The adapter key for the skip to previous control.

static final int

The initial (level 0) fast forward playback speed.

static final int

The level 1 fast forward playback speed.

static final int

The level 2 fast forward playback speed.

static final int

The level 3 fast forward playback speed.

static final int

The level 4 fast forward playback speed.

static final int

Invalid playback speed.

static final int

Speed representing playback state that is playing normally.

static final int

Speed representing playback state that is paused.

Public constructors

PlaybackBannerControlGlue(
    @NonNull Context context,
    @NonNull int[] seekSpeeds,
    T impl
)

Constructor for the glue.

PlaybackBannerControlGlue(
    @NonNull Context context,
    @NonNull int[] fastForwardSpeeds,
    @NonNull int[] rewindSpeeds,
    T impl
)

Constructor for the glue.

Public methods

long

Gets current position of the player.

@NonNull int[]

Returns the fast forward speeds.

@NonNull int[]

Returns the rewind speeds.

void

Handles action clicks.

boolean
onKey(View v, int keyCode, KeyEvent event)

Handles key events and returns true if handled.

void

Pauses the media player.

void

Starts the media player.

void

Sets the controls row to be managed by the glue layer.

Protected methods

void
onCreatePrimaryActions(
    @NonNull ArrayObjectAdapter primaryActionsAdapter
)

May be overridden to add primary actions to the adapter.

@NonNull PlaybackRowPresenter
void

Event when play finishes, subclass may handling repeat mode here.

void

Event when play state changed.

Inherited Constants

From androidx.leanback.media.PlaybackBaseControlGlue
static final int

The adapter key for the first custom control on the left side of the predefined primary controls.

static final int

The adapter key for the first custom control on the right side of the predefined primary controls.

static final int

The adapter key for the fast forward control.

static final int

The adapter key for the play/pause control.

static final int

The adapter key for the repeat control.

static final int

The adapter key for the rewind control.

static final int

The adapter key for the shuffle control.

static final int

The adapter key for the skip to next control.

static final int

The adapter key for the skip to previous control.

Inherited methods

From androidx.leanback.media.PlaybackBaseControlGlue
@Nullable Drawable
final long
@Nullable PlaybackControlsRow

Returns the playback controls row managed by the glue layer.

final long
@Nullable PlaybackRowPresenter

Returns the playback controls row Presenter managed by the glue layer.

final T
@Nullable CharSequence

Return The media subtitle.

long

Returns a bitmask of actions supported by the media player.

@Nullable CharSequence

Returns the title of the media item.

boolean

Returns true if the controls auto hides after a timeout when media is playing.

final boolean

Returns true if media is currently playing.

final boolean

Returns true when the media player is prepared to start media playback.

void

Goes to the next media item.

static void
notifyItemChanged(
    @NonNull ArrayObjectAdapter adapter,
    @NonNull Object object
)
void

This method is called attached to associated PlaybackGlueHost.

void
onCreateSecondaryActions(
    @NonNull ArrayObjectAdapter secondaryActionsAdapter
)

May be overridden to add secondary actions to the adapter.

void

This method is called when current associated PlaybackGlueHost is attached to a different PlaybackGlue or PlaybackGlueHost is destroyed .

void

This method is called when PlaybackGlueHost is started.

void

This method is called when PlaybackGlueHost is stopped.

void

Event when metadata changed

void

Event when ready state for play changes.

void
void
void
void

Goes to the previous media item.

final void
seekTo(long position)

Seek media to a new position.

void

Sets the drawable representing cover image.

void

Sets the controls to auto hide after a timeout when media is playing.

void

Sets the controls row Presenter to be managed by the glue layer.

void

Sets the media subtitle.

void

Sets the media title.

From androidx.leanback.media.PlaybackGlue
void

Add a PlayerCallback.

@NonNull Context

Returns the context.

@Nullable PlaybackGlueHost
@Nullable List<PlaybackGlue.PlayerCallback>
void

This method is called when PlaybackGlueHost is paused.

void

This method is called when PlaybackGlueHost is resumed.

void

Starts play when isPrepared becomes true.

void

Remove a PlayerCallback.

final void

This method is used to associate a PlaybackGlue with the PlaybackGlueHost which provides UI and optional SurfaceHolderGlueHost.

From android.view.View.OnKeyListener
abstract boolean
onKey(View p, int p1, KeyEvent p2)

Constants

ACTION_CUSTOM_LEFT_FIRST

Added in 1.1.0
public static final int ACTION_CUSTOM_LEFT_FIRST = 1

The adapter key for the first custom control on the left side of the predefined primary controls.

ACTION_CUSTOM_RIGHT_FIRST

Added in 1.1.0
public static final int ACTION_CUSTOM_RIGHT_FIRST = 4096

The adapter key for the first custom control on the right side of the predefined primary controls.

ACTION_FAST_FORWARD

Added in 1.1.0
public static final int ACTION_FAST_FORWARD = 128

The adapter key for the fast forward control.

ACTION_PLAY_PAUSE

Added in 1.1.0
public static final int ACTION_PLAY_PAUSE = 64

The adapter key for the play/pause control.

ACTION_REWIND

Added in 1.1.0
public static final int ACTION_REWIND = 32

The adapter key for the rewind control.

ACTION_SKIP_TO_NEXT

Added in 1.1.0
public static final int ACTION_SKIP_TO_NEXT = 256

The adapter key for the skip to next control.

ACTION_SKIP_TO_PREVIOUS

Added in 1.1.0
public static final int ACTION_SKIP_TO_PREVIOUS = 16

The adapter key for the skip to previous control.

PLAYBACK_SPEED_FAST_L0

Added in 1.1.0
public static final int PLAYBACK_SPEED_FAST_L0 = 10

The initial (level 0) fast forward playback speed. The negative of this value is for rewind at the same speed.

PLAYBACK_SPEED_FAST_L1

Added in 1.1.0
public static final int PLAYBACK_SPEED_FAST_L1 = 11

The level 1 fast forward playback speed. The negative of this value is for rewind at the same speed.

PLAYBACK_SPEED_FAST_L2

Added in 1.1.0
public static final int PLAYBACK_SPEED_FAST_L2 = 12

The level 2 fast forward playback speed. The negative of this value is for rewind at the same speed.

PLAYBACK_SPEED_FAST_L3

Added in 1.1.0
public static final int PLAYBACK_SPEED_FAST_L3 = 13

The level 3 fast forward playback speed. The negative of this value is for rewind at the same speed.

PLAYBACK_SPEED_FAST_L4

Added in 1.1.0
public static final int PLAYBACK_SPEED_FAST_L4 = 14

The level 4 fast forward playback speed. The negative of this value is for rewind at the same speed.

PLAYBACK_SPEED_INVALID

Added in 1.1.0
public static final int PLAYBACK_SPEED_INVALID = -1

Invalid playback speed.

PLAYBACK_SPEED_NORMAL

Added in 1.1.0
public static final int PLAYBACK_SPEED_NORMAL = 1

Speed representing playback state that is playing normally.

PLAYBACK_SPEED_PAUSED

Added in 1.1.0
public static final int PLAYBACK_SPEED_PAUSED = 0

Speed representing playback state that is paused.

Public constructors

PlaybackBannerControlGlue

Added in 1.1.0
public PlaybackBannerControlGlue(
    @NonNull Context context,
    @NonNull int[] seekSpeeds,
    T impl
)

Constructor for the glue.

Parameters
@NonNull Context context
@NonNull int[] seekSpeeds

The array of seek speeds for fast forward and rewind. The maximum length of the array is defined as NUMBER_OF_SEEK_SPEEDS.

T impl

Implementation to underlying media player.

PlaybackBannerControlGlue

Added in 1.1.0
public PlaybackBannerControlGlue(
    @NonNull Context context,
    @NonNull int[] fastForwardSpeeds,
    @NonNull int[] rewindSpeeds,
    T impl
)

Constructor for the glue.

Parameters
@NonNull Context context
@NonNull int[] fastForwardSpeeds

The array of seek speeds for fast forward. The maximum length of the array is defined as NUMBER_OF_SEEK_SPEEDS.

@NonNull int[] rewindSpeeds

The array of seek speeds for rewind. The maximum length of the array is defined as NUMBER_OF_SEEK_SPEEDS.

T impl

Implementation to underlying media player.

Public methods

getCurrentPosition

public long getCurrentPosition()

Gets current position of the player. If the player is playing/paused, this method returns current position from PlayerAdapter. Otherwise, if the player is fastforwarding/rewinding, the method fake-pauses the PlayerAdapter and returns its own calculated position.

Returns
long

Current position of the player.

getFastForwardSpeeds

Added in 1.1.0
public @NonNull int[] getFastForwardSpeeds()

Returns the fast forward speeds.

getRewindSpeeds

Added in 1.1.0
public @NonNull int[] getRewindSpeeds()

Returns the rewind speeds.

onActionClicked

Added in 1.2.0-alpha04
public void onActionClicked(@NonNull Action action)

Handles action clicks. A subclass may override this add support for additional actions.

onKey

Added in 1.1.0
public boolean onKey(View v, int keyCode, KeyEvent event)

Handles key events and returns true if handled. A subclass may override this to provide additional support.

pause

public void pause()

Pauses the media player.

play

public void play()

Starts the media player. Does nothing if isPrepared is false. To wait isPrepared to be true before playing, use playWhenPrepared.

setControlsRow

public void setControlsRow(@NonNull PlaybackControlsRow controlsRow)

Sets the controls row to be managed by the glue layer. If getPrimaryActionsAdapter is not provided, a default ArrayObjectAdapter will be created and initialized in onCreatePrimaryActions. If getSecondaryActionsAdapter is not provided, a default ArrayObjectAdapter will be created and initialized in onCreateSecondaryActions. The primary actions and playback state related aspects of the row are updated by the glue.

Protected methods

onCreatePrimaryActions

protected void onCreatePrimaryActions(
    @NonNull ArrayObjectAdapter primaryActionsAdapter
)

May be overridden to add primary actions to the adapter. Default implementation add PlaybackControlsRow.PlayPauseAction.

Parameters
@NonNull ArrayObjectAdapter primaryActionsAdapter

The adapter to add primary Actions.

onCreateRowPresenter

Added in 1.2.0-alpha04
protected @NonNull PlaybackRowPresenter onCreateRowPresenter()

onPlayCompleted

protected void onPlayCompleted()

Event when play finishes, subclass may handling repeat mode here.

onPlayStateChanged

protected void onPlayStateChanged()

Event when play state changed.