MediaPlayerAdapter


public class MediaPlayerAdapter extends PlayerAdapter


This implementation extends the PlayerAdapter with a MediaPlayer.

Summary

Public constructors

Constructor.

Public methods

long

Returns the current buffered position of the media item in milliseconds.

long

Returns the current position of the media item in milliseconds.

long

Returns the duration of the media item in milliseconds.

final MediaPlayer

Return the MediaPlayer associated with the MediaPlayerAdapter.

int

Return updating interval of progress UI in milliseconds.

boolean

Returns true if media is currently playing.

boolean
void

This method is called attached to associated PlaybackGlueHost.

void

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

void

Pauses the media player.

void

Starts the media player.

void

Release internal MediaPlayer.

void

Will reset the MediaPlayer and the glue such that a new file can be played.

void
seekTo(long positionInMs)

Seek to new position.

boolean

Sets the media source of the player witha given URI.

void

Implement this method to enable or disable progress updating.

Protected methods

boolean
onError(int what, int extra)

Called to indicate an error.

boolean
onInfo(int what, int extra)

Called to indicate an info or a warning.

void

Called to indicate the completion of a seek operation.

Inherited methods

From androidx.leanback.media.PlayerAdapter
void

Optional method.

final @Nullable PlayerAdapter.Callback

Gets callback for event of PlayerAdapter.

long

Return xor combination of values defined in PlaybackBaseControlGlue.

void

Optional method.

void

Optional method.

void

Optional method.

final void

Sets callback for event of PlayerAdapter.

void
setRepeatAction(int repeatActionIndex)

Optional method.

void
setShuffleAction(int shuffleActionIndex)

Optional method.

Public constructors

MediaPlayerAdapter

Added in 1.1.0
public MediaPlayerAdapter(Context context)

Constructor.

Public methods

getBufferedPosition

public long getBufferedPosition()

Returns the current buffered position of the media item in milliseconds.

getCurrentPosition

public long getCurrentPosition()

Returns the current position of the media item in milliseconds.

getDuration

public long getDuration()

Returns the duration of the media item in milliseconds.

getMediaPlayer

Added in 1.1.0
public final MediaPlayer getMediaPlayer()

Return the MediaPlayer associated with the MediaPlayerAdapter. App can use the instance to config DRM or control volumes, etc. Warning: App should not use the following seven listeners as they are controlled by MediaPlayerAdapter. If that's the case, app should write its own PlayerAdapter class. setOnPreparedListenersetOnVideoSizeChangedListenersetOnErrorListenersetOnSeekCompleteListenersetOnCompletionListenersetOnInfoListenersetOnBufferingUpdateListener

Returns
MediaPlayer

The MediaPlayer associated with the MediaPlayerAdapter.

getProgressUpdatingInterval

Added in 1.1.0
public int getProgressUpdatingInterval()

Return updating interval of progress UI in milliseconds. Subclass may override.

Returns
int

Update interval of progress UI in milliseconds.

isPlaying

public boolean isPlaying()

Returns true if media is currently playing.

isPrepared

public boolean isPrepared()
Returns
boolean

True if MediaPlayer OnPreparedListener is invoked and got a SurfaceHolder if PlaybackGlueHost provides SurfaceHolder.

onAttachedToHost

public void onAttachedToHost(@NonNull PlaybackGlueHost host)

This method is called attached to associated PlaybackGlueHost.

Parameters
@NonNull PlaybackGlueHost host

onDetachedFromHost

public void onDetachedFromHost()

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

pause

Added in 1.2.0-alpha04
public void pause()

Pauses the media player.

play

Added in 1.2.0-alpha04
public void play()

Starts the media player.

release

Added in 1.1.0
public void release()

Release internal MediaPlayer. Should not use the object after call release().

reset

Added in 1.1.0
public void reset()

Will reset the MediaPlayer and the glue such that a new file can be played. You are not required to call this method before playing the first file. However you have to call it before playing a second one.

seekTo

public void seekTo(long positionInMs)

Seek to new position.

Parameters
long positionInMs

New position in milliseconds.

setDataSource

Added in 1.1.0
public boolean setDataSource(Uri uri)

Sets the media source of the player witha given URI.

Returns
boolean

Returns true if uri represents a new media; falseotherwise.

See also
setDataSource

setProgressUpdatingEnabled

public void setProgressUpdatingEnabled(boolean enable)

Implement this method to enable or disable progress updating.

Parameters
boolean enable

True to enable progress updating, false otherwise.

Protected methods

onError

Added in 1.1.0
protected boolean onError(int what, int extra)

Called to indicate an error.

Parameters
int what

the type of error that has occurred:

int extra

an extra code, specific to the error. Typically implementation dependent.

Returns
boolean

True if the method handled the error, false if it didn't. Returning false, will cause the onPlayCompleted being called.

onInfo

Added in 1.1.0
protected boolean onInfo(int what, int extra)

Called to indicate an info or a warning.

Parameters
int what

the type of info or warning.

int extra

an extra code, specific to the info. Typically implementation dependent.

Returns
boolean

True if the method handled the info, false if it didn't. Returning false, will cause the info to be discarded.

onSeekComplete

Added in 1.1.0
protected void onSeekComplete()

Called to indicate the completion of a seek operation.