Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

PlaybackControlGlue

abstract class PlaybackControlGlue : PlaybackGlue, OnActionClickedListener, OnKeyListener
kotlin.Any
   ↳ androidx.leanback.media.PlaybackGlue
   ↳ androidx.leanback.media.PlaybackControlGlue

A helper class for managing a PlaybackControlsRow and PlaybackGlueHost that implements a recommended approach to handling standard playback control actions such as play/pause, fast forward/rewind at progressive speed levels, and skip to next/previous. This helper class is a glue layer in that manages the configuration of and interaction between the leanback UI components by defining a functional interface to the media player.

You can instantiate a concrete subclass such as MediaPlayerGlue or you must subclass this abstract helper. To create a subclass you must implement all of the abstract methods and the subclass must invoke onMetadataChanged() and onStateChanged() appropriately.

To use an instance of the glue layer, first construct an instance. Constructor parameters inform the glue what speed levels are supported for fast forward/rewind.

You may override onCreateControlsRowAndPresenter() which will create a PlaybackControlsRow and a PlaybackControlsRowPresenter. You may call setControlsRow(PlaybackControlsRow) and setPlaybackRowPresenter(PlaybackRowPresenter) to customize your own row and presenter.

The helper sets a SparseArrayObjectAdapter on the controls row as the primary actions adapter, and adds actions to it. You can provide additional actions by overriding onCreatePrimaryActions. This helper does not deal in secondary actions so those you may add separately.

Provide a click listener on your fragment and if an action is clicked, call onActionClicked.

This helper implements a key event handler. If you pass a PlaybackGlueHost, it will configure its fragment to intercept all key events. Otherwise, you should set the glue object as key event handler to the ViewHolder when bound by your row presenter; see RowPresenter.ViewHolder#setOnKeyListener(android.view.View.OnKeyListener).

To update the controls row progress during playback, override enableProgressUpdating to manage the lifecycle of a periodic callback to updateProgress(). getUpdatePeriod() provides a recommended update period.

Summary

Constants
static Int

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

static Int

The adapter key for the skip to previous control.

static Int

The adapter key for the rewind control.

static Int

The adapter key for the play/pause control.

static Int

The adapter key for the fast forward control.

static Int

The adapter key for the skip to next control.

static Int

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

static Int

Invalid playback speed.

static Int

Speed representing playback state that is paused.

static Int

Speed representing playback state that is playing normally.

static Int

The initial (level 0) fast forward playback speed.

static Int

The level 1 fast forward playback speed.

static Int

The level 2 fast forward playback speed.

static Int

The level 3 fast forward playback speed.

static Int

The level 4 fast forward playback speed.

Public constructors
<init>(context: Context!, seekSpeeds: IntArray!)

Constructor for the glue.

<init>(context: Context!, fastForwardSpeeds: IntArray!, rewindSpeeds: IntArray!)

Constructor for the glue.

Public methods
open PlaybackControlsRowPresenter!

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

abstract Int

Returns the current position of the media item in milliseconds.

open Unit
play(speed: Int)

Start playback at the given speed.

Unit

open IntArray!

Returns the fast forward speeds.

open PlaybackRowPresenter!

Returns the playback row Presenter to be passed to PlaybackGlueHost in onAttachedToHost(PlaybackGlueHost).

open Boolean
onKey(v: View!, keyCode: Int, event: KeyEvent!)

Handles key events and returns true if handled.

open IntArray!

Returns the rewind speeds.

abstract Long

Returns a bitmask of actions supported by the media player.

abstract Int

Returns the duration of the media item in milliseconds.

open Unit

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

abstract CharSequence!

Returns the title of the media item.

abstract CharSequence!

Returns the subtitle of the media item.

open Unit

Override this to start/stop a runnable to call updateProgress at an interval such as getUpdatePeriod.

abstract Drawable!

Returns a bitmap of the art for the media item.

open Unit

Updates the progress bar based on the current media playback position.

open Boolean

Returns true if controls are set to fade when media is playing.

open Unit

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

open Unit

Handles action clicks.

abstract Boolean

Returns true if there is a valid media item.

abstract Int

Returns the current playback speed.

open Unit

Sets the controls row Presenter to be passed to PlaybackGlueHost in onAttachedToHost(PlaybackGlueHost).

open Boolean

open Int

Returns the time period in milliseconds that should be used to update the progress.

open Unit

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

abstract Boolean

Returns true if media is currently playing.

open PlaybackControlsRow!

Returns the playback controls row managed by the glue layer.

Protected methods
open Unit
onCreateSecondaryActions(secondaryActionsAdapter: ArrayObjectAdapter!)

May be overridden to add secondary actions to the adapter.

open Unit

open Unit

Instantiating a PlaybackControlsRow and corresponding PlaybackControlsRowPresenter.

open Unit

open Unit

May be overridden to add primary actions to the adapter.

open Unit

open Unit

Must be called appropriately by a subclass when the metadata state has changed.

open Unit

Must be called appropriately by a subclass when the playback state has changed.

open Unit

Inherited functions

Constants

ACTION_CUSTOM_LEFT_FIRST

static val ACTION_CUSTOM_LEFT_FIRST: Int

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

Value: 0x1

ACTION_SKIP_TO_PREVIOUS

static val ACTION_SKIP_TO_PREVIOUS: Int

The adapter key for the skip to previous control.

Value: 0x10

ACTION_REWIND

static val ACTION_REWIND: Int

The adapter key for the rewind control.

Value: 0x20

ACTION_PLAY_PAUSE

static val ACTION_PLAY_PAUSE: Int

The adapter key for the play/pause control.

Value: 0x40

ACTION_FAST_FORWARD

static val ACTION_FAST_FORWARD: Int

The adapter key for the fast forward control.

Value: 0x80

ACTION_SKIP_TO_NEXT

static val ACTION_SKIP_TO_NEXT: Int

The adapter key for the skip to next control.

Value: 0x100

ACTION_CUSTOM_RIGHT_FIRST

static val ACTION_CUSTOM_RIGHT_FIRST: Int

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

Value: 0x1000

PLAYBACK_SPEED_INVALID

static val PLAYBACK_SPEED_INVALID: Int

Invalid playback speed.

Value: -1

PLAYBACK_SPEED_PAUSED

static val PLAYBACK_SPEED_PAUSED: Int

Speed representing playback state that is paused.

Value: 0

PLAYBACK_SPEED_NORMAL

static val PLAYBACK_SPEED_NORMAL: Int

Speed representing playback state that is playing normally.

Value: 1

PLAYBACK_SPEED_FAST_L0

static val PLAYBACK_SPEED_FAST_L0: Int

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

Value: 10

PLAYBACK_SPEED_FAST_L1

static val PLAYBACK_SPEED_FAST_L1: Int

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

Value: 11

PLAYBACK_SPEED_FAST_L2

static val PLAYBACK_SPEED_FAST_L2: Int

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

Value: 12

PLAYBACK_SPEED_FAST_L3

static val PLAYBACK_SPEED_FAST_L3: Int

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

Value: 13

PLAYBACK_SPEED_FAST_L4

static val PLAYBACK_SPEED_FAST_L4: Int

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

Value: 14

Public constructors

<init>

PlaybackControlGlue(context: Context!, seekSpeeds: IntArray!)

Constructor for the glue.

Parameters
context Context!:
seekSpeeds Context!: Array of seek speeds for fast forward and rewind.

<init>

PlaybackControlGlue(context: Context!, fastForwardSpeeds: IntArray!, rewindSpeeds: IntArray!)

Constructor for the glue.

Parameters
context Context!:
fastForwardSpeeds Context!: Array of seek speeds for fast forward.
rewindSpeeds Context!: Array of seek speeds for rewind.

Public methods

getControlsRowPresenter

open fun getControlsRowPresenter(): PlaybackControlsRowPresenter!

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

getCurrentPosition

abstract fun getCurrentPosition(): Int

Returns the current position of the media item in milliseconds.

play

open fun play(speed: Int): Unit

Start playback at the given speed.

Parameters
speed Int: The desired playback speed. For normal playback this will be PLAYBACK_SPEED_NORMAL; higher positive values for fast forward, and negative values for rewind.

play

fun play(): Unit

getFastForwardSpeeds

open fun getFastForwardSpeeds(): IntArray!

Returns the fast forward speeds.

getPlaybackRowPresenter

open fun getPlaybackRowPresenter(): PlaybackRowPresenter!

Returns the playback row Presenter to be passed to PlaybackGlueHost in onAttachedToHost(PlaybackGlueHost).

onKey

open fun onKey(v: View!, keyCode: Int, event: KeyEvent!): Boolean

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

getRewindSpeeds

open fun getRewindSpeeds(): IntArray!

Returns the rewind speeds.

getSupportedActions

abstract fun getSupportedActions(): Long

Returns a bitmask of actions supported by the media player.

getMediaDuration

abstract fun getMediaDuration(): Int

Returns the duration of the media item in milliseconds.

setFadingEnabled

open fun setFadingEnabled(enable: Boolean): Unit

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

getMediaTitle

abstract fun getMediaTitle(): CharSequence!

Returns the title of the media item.

getMediaSubtitle

abstract fun getMediaSubtitle(): CharSequence!

Returns the subtitle of the media item.

enableProgressUpdating

open fun enableProgressUpdating(enable: Boolean): Unit

Override this to start/stop a runnable to call updateProgress at an interval such as getUpdatePeriod.

getMediaArt

abstract fun getMediaArt(): Drawable!

Returns a bitmap of the art for the media item.

updateProgress

open fun updateProgress(): Unit

Updates the progress bar based on the current media playback position.

isFadingEnabled

open fun isFadingEnabled(): Boolean

Returns true if controls are set to fade when media is playing.

setControlsRow

open fun setControlsRow(controlsRow: PlaybackControlsRow!): Unit

Sets the controls row to be managed by the glue layer. The primary actions and playback state related aspects of the row are updated by the glue.

onActionClicked

open fun onActionClicked(action: Action!): Unit

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

hasValidMedia

abstract fun hasValidMedia(): Boolean

Returns true if there is a valid media item.

getCurrentSpeedId

abstract fun getCurrentSpeedId(): Int

Returns the current playback speed. When playing normally, PLAYBACK_SPEED_NORMAL should be returned.

setPlaybackRowPresenter

open fun setPlaybackRowPresenter(presenter: PlaybackRowPresenter!): Unit

Sets the controls row Presenter to be passed to PlaybackGlueHost in onAttachedToHost(PlaybackGlueHost).

isPlaying

open fun isPlaying(): Boolean

getUpdatePeriod

open fun getUpdatePeriod(): Int

Returns the time period in milliseconds that should be used to update the progress. See updateProgress().

setControlsRowPresenter

open fun setControlsRowPresenter(presenter: PlaybackControlsRowPresenter!): Unit

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

isMediaPlaying

abstract fun isMediaPlaying(): Boolean

Returns true if media is currently playing.

getControlsRow

open fun getControlsRow(): PlaybackControlsRow!

Returns the playback controls row managed by the glue layer.

Protected methods

onCreateSecondaryActions

protected open fun onCreateSecondaryActions(secondaryActionsAdapter: ArrayObjectAdapter!): Unit

May be overridden to add secondary actions to the adapter.

Parameters
secondaryActionsAdapter ArrayObjectAdapter!: The adapter you need to add the Actions to.

onHostStop

protected open fun onHostStop(): Unit

onCreateControlsRowAndPresenter

protected open fun onCreateControlsRowAndPresenter(): Unit

Instantiating a PlaybackControlsRow and corresponding PlaybackControlsRowPresenter. Subclass may override.

onHostStart

protected open fun onHostStart(): Unit

onCreatePrimaryActions

protected open fun onCreatePrimaryActions(primaryActionsAdapter: SparseArrayObjectAdapter!): Unit

May be overridden to add primary actions to the adapter.

Parameters
primaryActionsAdapter SparseArrayObjectAdapter!: The adapter to add primary Actions.

onDetachedFromHost

protected open fun onDetachedFromHost(): Unit

onMetadataChanged

protected open fun onMetadataChanged(): Unit

Must be called appropriately by a subclass when the metadata state has changed.

onStateChanged

protected open fun onStateChanged(): Unit

Must be called appropriately by a subclass when the playback state has changed. It updates the playback state displayed on the media player.

onAttachedToHost

protected open fun onAttachedToHost(host: PlaybackGlueHost!): Unit