PlaybackTransportControlGlue


public class PlaybackTransportControlGlue<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 in that manages interaction between the leanback UI components PlaybackControlsRowPlaybackTransportRowPresenter and a functional PlayerAdapter which represents the underlying media player.

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

The glue has two actions bar: primary actions bar and secondary actions bar. App can provide additional actions by overriding onCreatePrimaryActions and / or onCreateSecondaryActions and respond to actions by override onActionClicked.

It's also subclass's responsibility to implement the "repeat mode" in onPlayCompleted.

Apps calls setSeekProvider to provide seek data. If the PlaybackGlueHost is instance of PlaybackSeekUi, the provider will be passed to PlaybackGlueHost to render thumb bitmaps.

Sample Code:
public class MyVideoFragment extends VideoFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PlaybackTransportControlGlueplayerGlue =
                new PlaybackTransportControlGlue(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

Public constructors

Constructor for the glue.

Public methods

final PlaybackSeekDataProvider

Get seek data provider used during user seeking.

final boolean
void

Handles action clicks.

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

Handles key events and returns true if handled.

void

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

final void
setSeekEnabled(boolean seekEnabled)

Enable or disable seek when getSeekProvider is null.

final void

Set seek data provider used during user seeking.

Protected methods

void

This method is called attached to associated PlaybackGlueHost.

void
onCreatePrimaryActions(
    @NonNull ArrayObjectAdapter primaryActionsAdapter
)

May be overridden to add primary actions to the adapter.

@NonNull PlaybackRowPresenter
void

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

void

Event when play state changed.

void

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.

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.

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

May be overridden to add secondary actions to the adapter.

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 ready state for play changes.

void
void
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 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)

Public constructors

PlaybackTransportControlGlue

Added in 1.1.0
public PlaybackTransportControlGlue(Context context, T impl)

Constructor for the glue.

Parameters
Context context
T impl

Implementation to underlying media player.

Public methods

getSeekProvider

Added in 1.1.0
public final PlaybackSeekDataProvider getSeekProvider()

Get seek data provider used during user seeking.

Returns
PlaybackSeekDataProvider

Seek data provider used during user seeking.

isSeekEnabled

Added in 1.1.0
public final boolean isSeekEnabled()
Returns
boolean

True if seek is enabled without PlaybackSeekDataProvider, false otherwise.

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.

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.

setSeekEnabled

Added in 1.1.0
public final void setSeekEnabled(boolean seekEnabled)

Enable or disable seek when getSeekProvider is null. When true, seekTo will be called during user seeking.

Parameters
boolean seekEnabled

True to enable seek, false otherwise

setSeekProvider

Added in 1.1.0
public final void setSeekProvider(PlaybackSeekDataProvider seekProvider)

Set seek data provider used during user seeking.

Parameters
PlaybackSeekDataProvider seekProvider

Seek data provider used during user seeking.

Protected methods

onAttachedToHost

protected void onAttachedToHost(@NonNull PlaybackGlueHost host)

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

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()

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.

onPlayStateChanged

protected void onPlayStateChanged()

Event when play state changed.

onUpdateProgress

protected void onUpdateProgress()