PlaybackBaseControlGlue

public abstract class PlaybackBaseControlGlue<T extends PlayerAdapter> extends PlaybackGlue implements OnActionClickedListener, View.OnKeyListener

Known direct subclasses
PlaybackBannerControlGlue

A helper class for managing a PlaybackControlsRow being displayed in PlaybackGlueHost.

PlaybackTransportControlGlue

A helper class for managing a PlaybackControlsRow being displayed in PlaybackGlueHost, it supports standard playback control actions play/pause, and skip next/previous.


A base abstract 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 PlaybackControlsRowPlaybackRowPresenter and a functional PlayerAdapter which represents the underlying media player.

The app must pass a PlayerAdapter in 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.

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 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.

Public constructors

Constructor for the glue.

Public methods

@Nullable Drawable
final long
@Nullable PlaybackControlsRow

Returns the playback controls row managed by the glue layer.

long
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.

abstract void

Handles action clicks.

abstract 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

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 to be managed by the glue layer.

void

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

void

Sets the media subtitle.

void

Sets the media title.

Protected methods

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

This method is called attached to associated PlaybackGlueHost.

void
onCreatePrimaryActions(
    @NonNull ArrayObjectAdapter primaryActionsAdapter
)

May be overridden to add primary actions to the adapter.

abstract @NonNull PlaybackRowPresenter
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 play finishes, subclass may handling repeat mode here.

void

Event when play state changed.

void

Event when ready state for play changes.

void
void
void

Inherited methods

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.

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_REPEAT

Added in 1.1.0
public static final int ACTION_REPEAT = 512

The adapter key for the repeat control.

ACTION_REWIND

Added in 1.1.0
public static final int ACTION_REWIND = 32

The adapter key for the rewind control.

ACTION_SHUFFLE

Added in 1.1.0
public static final int ACTION_SHUFFLE = 1024

The adapter key for the shuffle 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.

Public constructors

PlaybackBaseControlGlue

Added in 1.1.0
public PlaybackBaseControlGlue(@NonNull Context context, T impl)

Constructor for the glue.

Parameters
@NonNull Context context
T impl

Implementation to underlying media player.

Public methods

getArt

Added in 1.1.0
public @Nullable Drawable getArt()
Returns
@Nullable Drawable

The drawable representing cover image.

getBufferedPosition

Added in 1.1.0
public final long getBufferedPosition()
Returns
long

The current buffered position of the media item in milliseconds.

getControlsRow

Added in 1.1.0
public @Nullable PlaybackControlsRow getControlsRow()

Returns the playback controls row managed by the glue layer.

getCurrentPosition

Added in 1.1.0
public long getCurrentPosition()
Returns
long

The current position of the media item in milliseconds.

getDuration

Added in 1.1.0
public final long getDuration()
Returns
long

The duration of the media item in milliseconds.

getPlaybackRowPresenter

Added in 1.1.0
public @Nullable PlaybackRowPresenter getPlaybackRowPresenter()

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

getPlayerAdapter

Added in 1.1.0
public final T getPlayerAdapter()

getSubtitle

Added in 1.1.0
public @Nullable CharSequence getSubtitle()

Return The media subtitle.

getSupportedActions

Added in 1.1.0
public long getSupportedActions()

Returns a bitmask of actions supported by the media player.

getTitle

Added in 1.1.0
public @Nullable CharSequence getTitle()

Returns the title of the media item.

isControlsOverlayAutoHideEnabled

Added in 1.1.0
public boolean isControlsOverlayAutoHideEnabled()

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

isPlaying

Added in 1.2.0-alpha04
public final boolean isPlaying()

Returns true if media is currently playing.

isPrepared

Added in 1.2.0-alpha04
public final boolean isPrepared()

Returns true when the media player is prepared to start media playback. When returning false, app may listen to onPreparedStateChanged event.

Returns
boolean

True if prepared, false otherwise.

next

public void next()

Goes to the next media item. This method is optional.

onActionClicked

public abstract void onActionClicked(@NonNull Action action)

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

onKey

public abstract 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.

previous

public void previous()

Goes to the previous media item. This method is optional.

seekTo

Added in 1.1.0
public final void seekTo(long position)

Seek media to a new position.

Parameters
long position

New position.

setArt

Added in 1.1.0
public void setArt(@Nullable Drawable cover)

Sets the drawable representing cover image. The drawable will be rendered by default description presenter in setDescriptionPresenter.

Parameters
@Nullable Drawable cover

The drawable representing cover image.

setControlsOverlayAutoHideEnabled

Added in 1.1.0
public void setControlsOverlayAutoHideEnabled(boolean enable)

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

Parameters
boolean enable

True to enable auto hide after a timeout when media is playing.

setControlsRow

Added in 1.1.0
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.

setPlaybackRowPresenter

Added in 1.1.0
public void setPlaybackRowPresenter(@Nullable PlaybackRowPresenter presenter)

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

setSubtitle

Added in 1.1.0
public void setSubtitle(@Nullable CharSequence subtitle)

Sets the media subtitle. The subtitle will be rendered by default description presenter setDescriptionPresenter.

Parameters
@Nullable CharSequence subtitle

Subtitle to set.

setTitle

Added in 1.1.0
public void setTitle(@Nullable CharSequence title)

Sets the media title. The title will be rendered by default description presenter setDescriptionPresenter.

Protected methods

notifyItemChanged

Added in 1.1.0
protected static void notifyItemChanged(
    @NonNull ArrayObjectAdapter adapter,
    @NonNull Object object
)

onAttachedToHost

protected void onAttachedToHost(@NonNull PlaybackGlueHost host)

This method is called attached to associated PlaybackGlueHost. Subclass may override and call super.onAttachedToHost().

onCreatePrimaryActions

Added in 1.1.0
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.1.0
protected abstract @NonNull PlaybackRowPresenter onCreateRowPresenter()

onCreateSecondaryActions

Added in 1.1.0
protected void onCreateSecondaryActions(
    @NonNull ArrayObjectAdapter secondaryActionsAdapter
)

May be overridden to add secondary actions to the adapter.

Parameters
@NonNull ArrayObjectAdapter secondaryActionsAdapter

The adapter you need to add the Actions to.

onDetachedFromHost

protected void onDetachedFromHost()

This method is called when current associated PlaybackGlueHost is attached to a different PlaybackGlue or PlaybackGlueHost is destroyed . Subclass may override and call super.onDetachedFromHost() at last. A typical PlaybackGlue will release resources (e.g. MediaPlayer or connection to playback service) in this method.

onHostStart

protected void onHostStart()

This method is called when PlaybackGlueHost is started. Subclass may override.

onHostStop

protected void onHostStop()

This method is called when PlaybackGlueHost is stopped. Subclass may override.

onMetadataChanged

Added in 1.1.0
protected void onMetadataChanged()

Event when metadata changed

onPlayCompleted

Added in 1.1.0
@CallSuper
protected void onPlayCompleted()

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

onPlayStateChanged

Added in 1.1.0
@CallSuper
protected void onPlayStateChanged()

Event when play state changed.

onPreparedStateChanged

Added in 1.1.0
@CallSuper
protected void onPreparedStateChanged()

Event when ready state for play changes.

onUpdateBufferedProgress

Added in 1.1.0
@CallSuper
protected void onUpdateBufferedProgress()

onUpdateDuration

Added in 1.1.0
@CallSuper
protected void onUpdateDuration()

onUpdateProgress

Added in 1.1.0
@CallSuper
protected void onUpdateProgress()