belongs to Maven artifact androidx.media2:media2:1.0.0-alpha01

BaseMediaPlayer

public abstract class BaseMediaPlayer
extends Object implements AutoCloseable

java.lang.Object
   ↳ androidx.media2.BaseMediaPlayer


Base interface for all media players that want media session.

Summary

Nested classes

class BaseMediaPlayer.PlayerEventCallback

A callback class to receive notifications for events on the media player. 

Constants

int BUFFERING_STATE_BUFFERING_AND_PLAYABLE

Buffering state indicating the player is buffering but enough has been buffered for this player to be able to play the content.

int BUFFERING_STATE_BUFFERING_AND_STARVED

Buffering state indicating the player is buffering, but the player is currently starved for data, and cannot play.

int BUFFERING_STATE_BUFFERING_COMPLETE

Buffering state indicating the player is done buffering, and the remainder of the content is available for playback.

int BUFFERING_STATE_UNKNOWN

Buffering state is unknown.

int PLAYER_STATE_ERROR

State when the player is in error state and cannot be recovered self.

int PLAYER_STATE_IDLE

State when the player is idle, and needs configuration to start playback.

int PLAYER_STATE_PAUSED

State when the player's playback is paused

int PLAYER_STATE_PLAYING

State when the player's playback is ongoing

long UNKNOWN_TIME

Public constructors

BaseMediaPlayer()

Public methods

abstract AudioAttributesCompat getAudioAttributes()

Returns AudioAttributes that media player has.

long getBufferedPosition()

Gets the buffered position of current playback, or UNKNOWN_TIME if unknown.

abstract int getBufferingState()

Returns the current buffering state of the player.

abstract DataSourceDesc2 getCurrentDataSource()

Returns the current data source.

long getCurrentPosition()

Gets the current playback head position.

long getDuration()

Returns the duration of the current data source, or UNKNOWN_TIME if unknown.

float getMaxPlayerVolume()
float getPlaybackSpeed()

Returns the actual playback speed to be used by the player when playing.

abstract int getPlayerState()

Returns the current player state.

abstract float getPlayerVolume()

Returns the current volume of this player to this player.

boolean isReversePlaybackSupported()

Indicates whether reverse playback is supported.

abstract void loopCurrent(boolean loop)

Configures the player to loop on the current data source.

abstract void pause()

Pauses playback.

abstract void play()

Starts or resumes playback.

abstract void prepare()

Prepares the player for playback.

abstract void registerPlayerEventCallback(Executor e, BaseMediaPlayer.PlayerEventCallback cb)

Adds a callback to be notified of events for this player.

abstract void reset()

Resets the BaseMediaPlayer to its uninitialized state.

abstract void seekTo(long pos)

Moves the playback head to the specified position

abstract void setAudioAttributes(AudioAttributesCompat attributes)

Sets the AudioAttributesCompat to be used during the playback of the media.

abstract void setDataSource(DataSourceDesc2 dsd)

Sets the data source to be played.

abstract void setNextDataSource(DataSourceDesc2 dsd)

Sets the data source that will be played immediately after the current one is done playing.

abstract void setNextDataSources(List<DataSourceDesc2> dsds)

Sets the list of data sources that will be sequentially played after the current one.

abstract void setPlaybackSpeed(float speed)

Sets the playback speed.

abstract void setPlayerVolume(float volume)

Sets the volume of the audio of the media to play, expressed as a linear multiplier on the audio samples.

abstract void skipToNext()

abstract void unregisterPlayerEventCallback(BaseMediaPlayer.PlayerEventCallback cb)

Removes a previously registered callback for player events

Inherited methods

Constants

BUFFERING_STATE_BUFFERING_AND_PLAYABLE

public static final int BUFFERING_STATE_BUFFERING_AND_PLAYABLE

Buffering state indicating the player is buffering but enough has been buffered for this player to be able to play the content. See getBufferedPosition() for how far is buffered already.

Constant Value: 1 (0x00000001)

BUFFERING_STATE_BUFFERING_AND_STARVED

public static final int BUFFERING_STATE_BUFFERING_AND_STARVED

Buffering state indicating the player is buffering, but the player is currently starved for data, and cannot play.

Constant Value: 2 (0x00000002)

BUFFERING_STATE_BUFFERING_COMPLETE

public static final int BUFFERING_STATE_BUFFERING_COMPLETE

Buffering state indicating the player is done buffering, and the remainder of the content is available for playback.

Constant Value: 3 (0x00000003)

BUFFERING_STATE_UNKNOWN

public static final int BUFFERING_STATE_UNKNOWN

Buffering state is unknown.

Constant Value: 0 (0x00000000)

PLAYER_STATE_ERROR

public static final int PLAYER_STATE_ERROR

State when the player is in error state and cannot be recovered self.

Constant Value: 3 (0x00000003)

PLAYER_STATE_IDLE

public static final int PLAYER_STATE_IDLE

State when the player is idle, and needs configuration to start playback.

Constant Value: 0 (0x00000000)

PLAYER_STATE_PAUSED

public static final int PLAYER_STATE_PAUSED

State when the player's playback is paused

Constant Value: 1 (0x00000001)

PLAYER_STATE_PLAYING

public static final int PLAYER_STATE_PLAYING

State when the player's playback is ongoing

Constant Value: 2 (0x00000002)

UNKNOWN_TIME

public static final long UNKNOWN_TIME

Constant Value: -1 (0xffffffffffffffff)

Public constructors

BaseMediaPlayer

public BaseMediaPlayer ()

Public methods

getAudioAttributes

public abstract AudioAttributesCompat getAudioAttributes ()

Returns AudioAttributes that media player has.

Returns
AudioAttributesCompat

getBufferedPosition

public long getBufferedPosition ()

Gets the buffered position of current playback, or UNKNOWN_TIME if unknown.

Returns
long the buffered position in ms, or UNKNOWN_TIME.

getBufferingState

public abstract int getBufferingState ()

Returns the current buffering state of the player. During buffering, see getBufferedPosition() for the quantifying the amount already buffered.

Returns
int the buffering state.

getCurrentDataSource

public abstract DataSourceDesc2 getCurrentDataSource ()

Returns the current data source.

Returns
DataSourceDesc2 the current data source, or null if none is set, or none available to play.

getCurrentPosition

public long getCurrentPosition ()

Gets the current playback head position.

Returns
long the current playback position in ms, or UNKNOWN_TIME if unknown.

getDuration

public long getDuration ()

Returns the duration of the current data source, or UNKNOWN_TIME if unknown.

Returns
long the duration in ms, or UNKNOWN_TIME.

getMaxPlayerVolume

public float getMaxPlayerVolume ()

Returns
float the maximum volume that can be used in setPlayerVolume(float).

getPlaybackSpeed

public float getPlaybackSpeed ()

Returns the actual playback speed to be used by the player when playing. Note that it may differ from the speed set in setPlaybackSpeed(float).

Returns
float the actual playback speed

getPlayerState

public abstract int getPlayerState ()

Returns the current player state. See also BaseMediaPlayer.PlayerEventCallback.onPlayerStateChanged(BaseMediaPlayer, int) for notification of changes.

Returns
int the current player state

getPlayerVolume

public abstract float getPlayerVolume ()

Returns the current volume of this player to this player. Note that it does not take into account the associated stream volume.

Returns
float the player volume.

isReversePlaybackSupported

public boolean isReversePlaybackSupported ()

Indicates whether reverse playback is supported. Reverse playback is indicated by negative playback speeds, see setPlaybackSpeed(float).

Returns
boolean true if reverse playback is supported.

loopCurrent

public abstract void loopCurrent (boolean loop)

Configures the player to loop on the current data source.

Parameters
loop boolean: true if the current data source is meant to loop.

pause

public abstract void pause ()

Pauses playback.

play

public abstract void play ()

Starts or resumes playback.

prepare

public abstract void prepare ()

Prepares the player for playback. See BaseMediaPlayer.PlayerEventCallback.onMediaPrepared(BaseMediaPlayer, DataSourceDesc2) for being notified when the preparation phase completed. During this time, the player may allocate resources required to play, such as audio and video decoders.

registerPlayerEventCallback

public abstract void registerPlayerEventCallback (Executor e, 
                BaseMediaPlayer.PlayerEventCallback cb)

Adds a callback to be notified of events for this player.

Parameters
e Executor: the Executor to be used for the events.

cb BaseMediaPlayer.PlayerEventCallback: the callback to receive the events.

reset

public abstract void reset ()

Resets the BaseMediaPlayer to its uninitialized state.

seekTo

public abstract void seekTo (long pos)

Moves the playback head to the specified position

Parameters
pos long: the new playback position expressed in ms.

setAudioAttributes

public abstract void setAudioAttributes (AudioAttributesCompat attributes)

Sets the AudioAttributesCompat to be used during the playback of the media. You must call this method before prepare() in order for the audio attributes to become effective thereafter.

Parameters
attributes AudioAttributesCompat: non-null AudioAttributes.

setDataSource

public abstract void setDataSource (DataSourceDesc2 dsd)

Sets the data source to be played.

setNextDataSource

public abstract void setNextDataSource (DataSourceDesc2 dsd)

Sets the data source that will be played immediately after the current one is done playing.

setNextDataSources

public abstract void setNextDataSources (List<DataSourceDesc2> dsds)

Sets the list of data sources that will be sequentially played after the current one. Each data source is played immediately after the previous one is done playing.

setPlaybackSpeed

public abstract void setPlaybackSpeed (float speed)

Sets the playback speed. A value of 1.0f is the default playback value. A negative value indicates reverse playback, check isReversePlaybackSupported() before using negative values.
After changing the playback speed, it is recommended to query the actual speed supported by the player, see getPlaybackSpeed().

setPlayerVolume

public abstract void setPlayerVolume (float volume)

Sets the volume of the audio of the media to play, expressed as a linear multiplier on the audio samples. Note that this volume is specific to the player, and is separate from stream volume used across the platform.
A value of 0.0f indicates muting, a value of 1.0f is the nominal unattenuated and unamplified gain. See getMaxPlayerVolume() for the volume range supported by this player.

Parameters
volume float: a value between 0.0f and getMaxPlayerVolume().

skipToNext

public abstract void skipToNext ()

unregisterPlayerEventCallback

public abstract void unregisterPlayerEventCallback (BaseMediaPlayer.PlayerEventCallback cb)

Removes a previously registered callback for player events

Parameters
cb BaseMediaPlayer.PlayerEventCallback: the callback to remove