PlaybackControlGlue


abstract class PlaybackControlGlue : PlaybackGlue, OnActionClickedListener, View.OnKeyListener


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 and setPlaybackRowPresenter 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 setOnKeyListener.

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

const Int

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

const Int

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

const Int

The adapter key for the fast forward control.

const Int

The adapter key for the play/pause control.

const Int

The adapter key for the rewind control.

const Int

The adapter key for the skip to next control.

const Int

The adapter key for the skip to previous control.

const Int

The initial (level 0) fast forward playback speed.

const Int

The level 1 fast forward playback speed.

const Int

The level 2 fast forward playback speed.

const Int

The level 3 fast forward playback speed.

const Int

The level 4 fast forward playback speed.

const Int

Invalid playback speed.

const Int

Speed representing playback state that is playing normally.

const Int

Speed representing playback state that is paused.

Public constructors

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

Constructor for the glue.

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

Constructor for the glue.

Public functions

Unit

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

PlaybackControlsRow!

Returns the playback controls row managed by the glue layer.

PlaybackControlsRowPresenter!

This function is deprecated.

PlaybackControlGlue supports any PlaybackRowPresenter, use getPlaybackRowPresenter.

abstract Int

Returns the current position of the media item in milliseconds.

abstract Int

Returns the current playback speed.

IntArray<Int>!

Returns the fast forward speeds.

abstract Drawable!

Returns a bitmap of the art for the media item.

abstract Int

Returns the duration of the media item in milliseconds.

abstract CharSequence!

Returns the subtitle of the media item.

abstract CharSequence!

Returns the title of the media item.

PlaybackRowPresenter!

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

IntArray<Int>!

Returns the rewind speeds.

abstract Long

Returns a bitmask of actions supported by the media player.

Int

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

abstract Boolean

Returns true if there is a valid media item.

Boolean

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

abstract Boolean

Returns true if media is currently playing.

Boolean

Returns true if media is currently playing.

Unit

Handles action clicks.

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

Handles key events and returns true if handled.

Unit

Starts the media player.

Unit
play(speed: Int)

Start playback at the given speed.

Unit

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

Unit

This function is deprecated.

PlaybackControlGlue supports any PlaybackRowPresenter, use setPlaybackRowPresenter.

Unit

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

Unit

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

Unit

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

Protected functions

Unit

This method is called attached to associated PlaybackGlueHost.

Unit

Instantiating a PlaybackControlsRow and corresponding PlaybackControlsRowPresenter.

Unit

May be overridden to add primary actions to the adapter.

Unit
onCreateSecondaryActions(secondaryActionsAdapter: ArrayObjectAdapter!)

May be overridden to add secondary actions to the adapter.

Unit

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

Unit

This method is called when PlaybackGlueHost is started.

Unit

This method is called when PlaybackGlueHost is stopped.

Unit

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

Unit

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

Inherited functions

From androidx.leanback.media.PlaybackGlue
Unit

Add a PlayerCallback.

Context

Returns the context.

PlaybackGlueHost?
(Mutable)List<PlaybackGlue.PlayerCallback!>?
Boolean

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

Unit

Goes to the next media item.

Unit

This method is called when PlaybackGlueHost is paused.

Unit

This method is called when PlaybackGlueHost is resumed.

Unit

Pauses the media player.

Unit

Starts play when isPrepared becomes true.

Unit

Goes to the previous media item.

Unit

Remove a PlayerCallback.

Unit

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
const val ACTION_CUSTOM_LEFT_FIRST = 1: Int

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
const val ACTION_CUSTOM_RIGHT_FIRST = 4096: Int

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
const val ACTION_FAST_FORWARD = 128: Int

The adapter key for the fast forward control.

ACTION_PLAY_PAUSE

Added in 1.1.0
const val ACTION_PLAY_PAUSE = 64: Int

The adapter key for the play/pause control.

ACTION_REWIND

Added in 1.1.0
const val ACTION_REWIND = 32: Int

The adapter key for the rewind control.

ACTION_SKIP_TO_NEXT

Added in 1.1.0
const val ACTION_SKIP_TO_NEXT = 256: Int

The adapter key for the skip to next control.

ACTION_SKIP_TO_PREVIOUS

Added in 1.1.0
const val ACTION_SKIP_TO_PREVIOUS = 16: Int

The adapter key for the skip to previous control.

PLAYBACK_SPEED_FAST_L0

Added in 1.1.0
const val PLAYBACK_SPEED_FAST_L0 = 10: Int

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
const val PLAYBACK_SPEED_FAST_L1 = 11: Int

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
const val PLAYBACK_SPEED_FAST_L2 = 12: Int

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
const val PLAYBACK_SPEED_FAST_L3 = 13: Int

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
const val PLAYBACK_SPEED_FAST_L4 = 14: Int

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
const val PLAYBACK_SPEED_INVALID = -1: Int

Invalid playback speed.

PLAYBACK_SPEED_NORMAL

Added in 1.1.0
const val PLAYBACK_SPEED_NORMAL = 1: Int

Speed representing playback state that is playing normally.

PLAYBACK_SPEED_PAUSED

Added in 1.1.0
const val PLAYBACK_SPEED_PAUSED = 0: Int

Speed representing playback state that is paused.

Public constructors

PlaybackControlGlue

Added in 1.1.0
PlaybackControlGlue(context: Context!, seekSpeeds: IntArray!)

Constructor for the glue.

Parameters
context: Context!
seekSpeeds: IntArray!

Array of seek speeds for fast forward and rewind.

PlaybackControlGlue

Added in 1.1.0
PlaybackControlGlue(
    context: Context!,
    fastForwardSpeeds: IntArray!,
    rewindSpeeds: IntArray!
)

Constructor for the glue.

Parameters
context: Context!
fastForwardSpeeds: IntArray!

Array of seek speeds for fast forward.

rewindSpeeds: IntArray!

Array of seek speeds for rewind.

Public functions

enableProgressUpdating

Added in 1.1.0
fun enableProgressUpdating(enable: Boolean): Unit

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

getControlsRow

Added in 1.1.0
fun getControlsRow(): PlaybackControlsRow!

Returns the playback controls row managed by the glue layer.

getControlsRowPresenter

Added in 1.1.0
Deprecated in 1.1.0
fun getControlsRowPresenter(): PlaybackControlsRowPresenter!

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

getCurrentPosition

Added in 1.1.0
abstract fun getCurrentPosition(): Int

Returns the current position of the media item in milliseconds.

getCurrentSpeedId

Added in 1.1.0
abstract fun getCurrentSpeedId(): Int

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

getFastForwardSpeeds

Added in 1.1.0
fun getFastForwardSpeeds(): IntArray<Int>!

Returns the fast forward speeds.

getMediaArt

Added in 1.1.0
abstract fun getMediaArt(): Drawable!

Returns a bitmap of the art for the media item.

getMediaDuration

Added in 1.1.0
abstract fun getMediaDuration(): Int

Returns the duration of the media item in milliseconds.

getMediaSubtitle

Added in 1.1.0
abstract fun getMediaSubtitle(): CharSequence!

Returns the subtitle of the media item.

getMediaTitle

Added in 1.1.0
abstract fun getMediaTitle(): CharSequence!

Returns the title of the media item.

getPlaybackRowPresenter

Added in 1.1.0
fun getPlaybackRowPresenter(): PlaybackRowPresenter!

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

getRewindSpeeds

Added in 1.1.0
fun getRewindSpeeds(): IntArray<Int>!

Returns the rewind speeds.

getSupportedActions

Added in 1.1.0
abstract fun getSupportedActions(): Long

Returns a bitmask of actions supported by the media player.

getUpdatePeriod

Added in 1.1.0
fun getUpdatePeriod(): Int

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

hasValidMedia

Added in 1.1.0
abstract fun hasValidMedia(): Boolean

Returns true if there is a valid media item.

isFadingEnabled

Added in 1.1.0
fun isFadingEnabled(): Boolean

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

isMediaPlaying

Added in 1.1.0
abstract fun isMediaPlaying(): Boolean

Returns true if media is currently playing.

isPlaying

fun isPlaying(): Boolean

Returns true if media is currently playing.

onActionClicked

Added in 1.2.0-alpha04
fun onActionClicked(action: Action!): Unit

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

onKey

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

play

Added in 1.2.0-alpha04
fun play(): Unit

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

play

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

setControlsRow

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

setControlsRowPresenter

Added in 1.1.0
Deprecated in 1.1.0
fun setControlsRowPresenter(presenter: PlaybackControlsRowPresenter!): Unit

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

setFadingEnabled

Added in 1.1.0
fun setFadingEnabled(enable: Boolean): Unit

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

setPlaybackRowPresenter

Added in 1.1.0
fun setPlaybackRowPresenter(presenter: PlaybackRowPresenter!): Unit

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

updateProgress

Added in 1.1.0
fun updateProgress(): Unit

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

Protected functions

onAttachedToHost

protected fun onAttachedToHost(host: PlaybackGlueHost): Unit

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

onCreateControlsRowAndPresenter

Added in 1.1.0
protected fun onCreateControlsRowAndPresenter(): Unit

Instantiating a PlaybackControlsRow and corresponding PlaybackControlsRowPresenter. Subclass may override.

onCreatePrimaryActions

Added in 1.1.0
protected fun onCreatePrimaryActions(primaryActionsAdapter: SparseArrayObjectAdapter!): Unit

May be overridden to add primary actions to the adapter.

Parameters
primaryActionsAdapter: SparseArrayObjectAdapter!

The adapter to add primary Actions.

onCreateSecondaryActions

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

onDetachedFromHost

protected fun onDetachedFromHost(): Unit

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 fun onHostStart(): Unit

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

onHostStop

protected fun onHostStop(): Unit

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

onMetadataChanged

Added in 1.1.0
protected fun onMetadataChanged(): Unit

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

onStateChanged

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