Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.
added in version 1.0.0
belongs to Maven artifact androidx.leanback:leanback:1.0.0-rc01

PlaybackGlue

public abstract class PlaybackGlue
extends Object

java.lang.Object
   ↳ androidx.leanback.media.PlaybackGlue


Base class for abstraction of media play/pause feature. A subclass of PlaybackGlue will contain implementation of Media Player or a connection to playback Service. App initializes PlaybackGlue subclass, associated it with a PlaybackGlueHost. PlaybackGlueHost is typically implemented by a Fragment or an Activity, it provides the environment to render UI for PlaybackGlue object, it optionally provides SurfaceHolder via SurfaceHolderGlueHost to render video. A typical PlaybackGlue should release resources (e.g. MediaPlayer or connection to playback Service) in onDetachedFromHost(). onDetachedFromHost() is called in two cases:

In rare case if an PlaybackGlue wants to live outside fragment / activity life cycle, it may manages resource release by itself.

See also:

Summary

Nested classes

class PlaybackGlue.PlayerCallback

Interface to allow clients to take action once the video is ready to play and start stop. 

Public constructors

PlaybackGlue(Context context)

Constructor.

Public methods

void addPlayerCallback(PlaybackGlue.PlayerCallback playerCallback)

Add a PlayerCallback.

Context getContext()

Returns the context.

PlaybackGlueHost getHost()
boolean isPlaying()

Returns true if media is currently playing.

boolean isPrepared()

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

void next()

Goes to the next media item.

void pause()

Pauses the media player.

void play()

Starts the media player.

void playWhenPrepared()

Starts play when isPrepared() becomes true.

void previous()

Goes to the previous media item.

void removePlayerCallback(PlaybackGlue.PlayerCallback callback)

Remove a PlayerCallback.

final void setHost(PlaybackGlueHost host)

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

Protected methods

List<PlaybackGlue.PlayerCallback> getPlayerCallbacks()
void onAttachedToHost(PlaybackGlueHost host)

This method is called attached to associated PlaybackGlueHost.

void onDetachedFromHost()

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

void onHostPause()

This method is called when {@link PlaybackGlueHost is paused.

void onHostResume()

This method is called when {@link PlaybackGlueHost is resumed.

void onHostStart()

This method is called when {@link PlaybackGlueHost is started.

void onHostStop()

This method is called when {@link PlaybackGlueHost is stopped.

Inherited methods

Public constructors

PlaybackGlue

added in version 1.0.0
public PlaybackGlue (Context context)

Constructor.

Parameters
context Context

Public methods

addPlayerCallback

added in version 1.0.0
public void addPlayerCallback (PlaybackGlue.PlayerCallback playerCallback)

Add a PlayerCallback.

Parameters
playerCallback PlaybackGlue.PlayerCallback: The callback to add.

getContext

added in version 1.0.0
public Context getContext ()

Returns the context.

Returns
Context

getHost

added in version 1.0.0
public PlaybackGlueHost getHost ()

Returns
PlaybackGlueHost Associated PlaybackGlueHost or null if not attached to host.

isPlaying

added in version 1.0.0
public boolean isPlaying ()

Returns true if media is currently playing.

Returns
boolean

isPrepared

added in version 1.0.0
public boolean isPrepared ()

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

Returns
boolean True if prepared, false otherwise.

next

added in version 1.0.0
public void next ()

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

pause

added in version 1.0.0
public void pause ()

Pauses the media player.

play

added in version 1.0.0
public void play ()

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

playWhenPrepared

added in version 1.0.0
public void playWhenPrepared ()

Starts play when isPrepared() becomes true.

previous

added in version 1.0.0
public void previous ()

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

removePlayerCallback

added in version 1.0.0
public void removePlayerCallback (PlaybackGlue.PlayerCallback callback)

Remove a PlayerCallback.

Parameters
callback PlaybackGlue.PlayerCallback: The callback to remove.

setHost

added in version 1.0.0
public final void setHost (PlaybackGlueHost host)

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

Parameters
host PlaybackGlueHost: The host for the PlaybackGlue. Set to null to detach from the host.

Protected methods

getPlayerCallbacks

added in version 1.0.0
protected List<PlaybackGlue.PlayerCallback> getPlayerCallbacks ()

Returns
List<PlaybackGlue.PlayerCallback> A snapshot of list of PlayerCallbacks set on the Glue.

onAttachedToHost

added in version 1.0.0
protected void onAttachedToHost (PlaybackGlueHost host)

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

Parameters
host PlaybackGlueHost

onDetachedFromHost

added in version 1.0.0
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.

onHostPause

added in version 1.0.0
protected void onHostPause ()

This method is called when {@link PlaybackGlueHost is paused. Subclass may override.

onHostResume

added in version 1.0.0
protected void onHostResume ()

This method is called when {@link PlaybackGlueHost is resumed. Subclass may override.

onHostStart

added in version 1.0.0
protected void onHostStart ()

This method is called when {@link PlaybackGlueHost is started. Subclass may override.

onHostStop

added in version 1.0.0
protected void onHostStop ()

This method is called when {@link PlaybackGlueHost is stopped. Subclass may override.