MediaPlayerInterface

abstract class MediaPlayerInterface : AutoCloseable
kotlin.Any
   ↳ androidx.media.MediaPlayerInterface

Base interface for all media players that want media session.

Summary

Nested classes
abstract

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

Constants
static Int

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

static Int

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

static Int

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

static Int

Buffering state is unknown.

static Int

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

static Int

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

static Int

State when the player's playback is paused

static Int

State when the player's playback is ongoing

static Long

Public constructors

Public methods
abstract Unit

abstract Unit

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

abstract Unit

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

open Long

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

abstract DataSourceDesc?

Returns the current data source.

open Float

abstract Unit

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

abstract Unit
seekTo(pos: Long)

Moves the playback head to the specified position

abstract Unit

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

abstract Int

Returns the current buffering state of the player.

abstract Unit

Sets the playback speed.

open Long

Gets the current playback head position.

abstract Unit

Starts or resumes playback.

abstract Unit

Removes a previously registered callback for player events

abstract Unit

Sets the data source to be played.

abstract Unit

Resets the MediaPlayerInterface to its uninitialized state.

abstract Unit

Configures the player to loop on the current data source.

abstract Unit

Prepares the player for playback.

abstract Int

Returns the current player state.

abstract Unit

Pauses playback.

abstract Unit

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

open Float

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

open Boolean

Indicates whether reverse playback is supported.

abstract AudioAttributesCompat?

Returns AudioAttributes that media player has.

open Long

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

abstract Float

Returns the current volume of this player to this player.

Constants

BUFFERING_STATE_BUFFERING_AND_PLAYABLE

static val BUFFERING_STATE_BUFFERING_AND_PLAYABLE: Int

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.

Value: 1

BUFFERING_STATE_BUFFERING_AND_STARVED

static val BUFFERING_STATE_BUFFERING_AND_STARVED: Int

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

Value: 2

BUFFERING_STATE_BUFFERING_COMPLETE

static val BUFFERING_STATE_BUFFERING_COMPLETE: Int

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

Value: 3

BUFFERING_STATE_UNKNOWN

static val BUFFERING_STATE_UNKNOWN: Int

Buffering state is unknown.

Value: 0

PLAYER_STATE_ERROR

static val PLAYER_STATE_ERROR: Int

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

Value: 3

PLAYER_STATE_IDLE

static val PLAYER_STATE_IDLE: Int

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

Value: 0

PLAYER_STATE_PAUSED

static val PLAYER_STATE_PAUSED: Int

State when the player's playback is paused

Value: 1

PLAYER_STATE_PLAYING

static val PLAYER_STATE_PLAYING: Int

State when the player's playback is ongoing

Value: 2

UNKNOWN_TIME

static val UNKNOWN_TIME: Long
Value: -1L

Public constructors

<init>

MediaPlayerInterface()

Public methods

skipToNext

abstract fun skipToNext(): Unit

setNextDataSource

abstract fun setNextDataSource(dsd: DataSourceDesc): Unit

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

Parameters
dsd DataSourceDesc:

setNextDataSources

abstract fun setNextDataSources(dsds: MutableList<DataSourceDesc!>): Unit

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.

Parameters
dsds MutableList<DataSourceDesc!>:

getDuration

open fun getDuration(): Long

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

Return
Long: the duration in ms, or #UNKNOWN_TIME.

getCurrentDataSource

abstract fun getCurrentDataSource(): DataSourceDesc?

Returns the current data source.

Return
DataSourceDesc?: the current data source, or null if none is set, or none available to play.

getMaxPlayerVolume

open fun getMaxPlayerVolume(): Float
Return
Float: the maximum volume that can be used in #setPlayerVolume(float).

registerPlayerEventCallback

abstract fun registerPlayerEventCallback(e: Executor, cb: MediaPlayerInterface.PlayerEventCallback): Unit

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

Parameters
e Executor: the Executor to be used for the events.
cb Executor: the callback to receive the events.

seekTo

abstract fun seekTo(pos: Long): Unit

Moves the playback head to the specified position

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

setPlayerVolume

abstract fun setPlayerVolume(volume: Float): Unit

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

getBufferingState

abstract fun getBufferingState(): Int

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

Return
Int: the buffering state.

setPlaybackSpeed

abstract fun setPlaybackSpeed(speed: Float): Unit

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

Parameters
speed Float:

getCurrentPosition

open fun getCurrentPosition(): Long

Gets the current playback head position.

Return
Long: the current playback position in ms, or #UNKNOWN_TIME if unknown.

play

abstract fun play(): Unit

Starts or resumes playback.

unregisterPlayerEventCallback

abstract fun unregisterPlayerEventCallback(cb: MediaPlayerInterface.PlayerEventCallback): Unit

Removes a previously registered callback for player events

Parameters
cb MediaPlayerInterface.PlayerEventCallback: the callback to remove

setDataSource

abstract fun setDataSource(dsd: DataSourceDesc): Unit

Sets the data source to be played.

Parameters
dsd DataSourceDesc:

reset

abstract fun reset(): Unit

Resets the MediaPlayerInterface to its uninitialized state.

loopCurrent

abstract fun loopCurrent(loop: Boolean): Unit

Configures the player to loop on the current data source.

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

prepare

abstract fun prepare(): Unit

Prepares the player for playback. See PlayerEventCallback#onMediaPrepared(MediaPlayerInterface, DataSourceDesc) 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.

getPlayerState

abstract fun getPlayerState(): Int

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

Return
Int: the current player state

pause

abstract fun pause(): Unit

Pauses playback.

setAudioAttributes

abstract fun setAudioAttributes(attributes: AudioAttributesCompat): Unit

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

Parameters
attributes AudioAttributesCompat: non-null AudioAttributes.

getPlaybackSpeed

open fun getPlaybackSpeed(): Float

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).

Return
Float: the actual playback speed

isReversePlaybackSupported

open fun isReversePlaybackSupported(): Boolean

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

Return
Boolean: true if reverse playback is supported.

getAudioAttributes

abstract fun getAudioAttributes(): AudioAttributesCompat?

Returns AudioAttributes that media player has.

getBufferedPosition

open fun getBufferedPosition(): Long

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

Return
Long: the buffered position in ms, or #UNKNOWN_TIME.

getPlayerVolume

abstract fun getPlayerVolume(): Float

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

Return
Float: the player volume.