lightbulb_outline Please take our October 2018 developer survey. Start survey
added in version 1.0.0
belongs to Maven artifact androidx.leanback:leanback:1.0.0

MediaPlayerAdapter

public class MediaPlayerAdapter
extends PlayerAdapter

java.lang.Object
   ↳ androidx.leanback.media.PlayerAdapter
     ↳ androidx.leanback.media.MediaPlayerAdapter


This implementation extends the PlayerAdapter with a MediaPlayer.

Summary

Public constructors

MediaPlayerAdapter(Context context)

Constructor.

Public methods

long getBufferedPosition()

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

long getCurrentPosition()

Returns the current position of the media item in milliseconds.

long getDuration()

Returns the duration of the media item in milliseconds.

final MediaPlayer getMediaPlayer()

Return the MediaPlayer associated with the MediaPlayerAdapter.

int getProgressUpdatingInterval()

Return updating interval of progress UI in milliseconds.

boolean isPlaying()

Returns true if media is currently playing.

boolean isPrepared()
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 pause()

Pauses the media player.

void play()

Starts the media player.

void release()

Release internal MediaPlayer.

void reset()

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

void seekTo(long newPosition)

Seek to new position.

boolean setDataSource(Uri uri)

Sets the media source of the player witha given URI.

void setProgressUpdatingEnabled(boolean enabled)

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

Called to indicate the completion of a seek operation.

Inherited methods

Public constructors

MediaPlayerAdapter

added in version 1.0.0
public MediaPlayerAdapter (Context context)

Constructor.

Parameters
context Context

Public methods

getBufferedPosition

added in version 1.0.0
public long getBufferedPosition ()

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

Returns
long

getCurrentPosition

added in version 1.0.0
public long getCurrentPosition ()

Returns the current position of the media item in milliseconds.

Returns
long

getDuration

added in version 1.0.0
public long getDuration ()

Returns the duration of the media item in milliseconds.

Returns
long

getMediaPlayer

added in version 1.0.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. MediaPlayer.setOnPreparedListener(MediaPlayer.OnPreparedListener) MediaPlayer.setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener) MediaPlayer.setOnErrorListener(MediaPlayer.OnErrorListener) MediaPlayer.setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener) MediaPlayer.setOnCompletionListener(MediaPlayer.OnCompletionListener) MediaPlayer.setOnInfoListener(MediaPlayer.OnInfoListener) MediaPlayer.setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener)

Returns
MediaPlayer The MediaPlayer associated with the MediaPlayerAdapter.

getProgressUpdatingInterval

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

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
boolean True if MediaPlayer OnPreparedListener is invoked and got a SurfaceHolder if PlaybackGlueHost provides SurfaceHolder.

onAttachedToHost

added in version 1.0.0
public void onAttachedToHost (PlaybackGlueHost host)

This method is called attached to associated PlaybackGlueHost.

onDetachedFromHost

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

release

added in version 1.0.0
public void release ()

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

reset

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

added in version 1.0.0
public void seekTo (long newPosition)

Seek to new position.

Parameters
newPosition long: New position in milliseconds.

setDataSource

added in version 1.0.0
public boolean setDataSource (Uri uri)

Sets the media source of the player witha given URI.

Parameters
uri Uri

Returns
boolean Returns true if uri represents a new media; false otherwise.

setProgressUpdatingEnabled

added in version 1.0.0
public void setProgressUpdatingEnabled (boolean enabled)

Implement this method to enable or disable progress updating.

Parameters
enabled boolean: True to enable progress updating, false otherwise.

Protected methods

onError

added in version 1.0.0
protected boolean onError (int what, 
                int extra)

Called to indicate an error.

Parameters
what int: the type of error that has occurred:

extra int: 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 PlayerAdapter.Callback.onPlayCompleted(PlayerAdapter) being called.

onInfo

added in version 1.0.0
protected boolean onInfo (int what, 
                int extra)

Called to indicate an info or a warning.

Parameters
what int: the type of info or warning.

extra int: 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 version 1.0.0
protected void onSeekComplete ()

Called to indicate the completion of a seek operation.