public interface Player

Known direct subclasses
BasePlayer

Abstract base Player which implements common implementation independent methods.

ExoPlayer

An extensible media player that plays MediaSources.

ForwardingPlayer

A Player that forwards method calls to another Player.

MediaController

A controller that interacts with a MediaSession, a MediaSessionService hosting a MediaSession, or a MediaLibraryService hosting a .

Known indirect subclasses
CastPlayer

Player implementation that communicates with a Cast receiver app.

ForwardingSimpleBasePlayer

A SimpleBasePlayer that forwards all calls to another Player instance.

MediaBrowser

Browses media content offered by a MediaLibraryService in addition to the functions.

SimpleBasePlayer

A base implementation for Player that reduces the number of methods to implement to a minimum.

SimpleExoPlayer

This class is deprecated.

Use ExoPlayer instead.

StubExoPlayer

An abstract ExoPlayer implementation that throws UnsupportedOperationException from every method.

StubPlayer

An abstract Player implementation that throws UnsupportedOperationException from every method.


A media player interface defining high-level functionality, such as the ability to play, pause, seek and query properties of the currently playing media.

Player features and usage

Some important properties of media players that implement this interface are:

  • All methods must be called from a single application thread unless indicated otherwise. Callbacks in registered listeners are called on the same thread.
  • The available functionality can be limited. Player instances provide a set of available commands to signal feature support and users of the interface must only call methods if the corresponding Command is available.
  • Users can register Player.Listener callbacks that get informed about state changes.
  • Player instances need to update the visible state immediately after each method call, even if the actual changes are handled on background threads or even other devices. This simplifies the usage for callers of methods as no asynchronous handling needs to be considered.
  • Player instances can provide playlist operations, like 'set', 'add', 'remove', 'move' or 'replace' of MediaItem instances. The player can also support repeat modes and shuffling within this playlist. The player provides a representing the structure of the playlist and all its items, which can be obtained by calling getCurrentTimeline
  • Player instances can provide seeking within the currently playing item and to other items, using the various seek... methods.
  • Player instances can provide Tracks defining the currently available and selected tracks, which can be obtained by calling getCurrentTracks. Users can also modify track selection behavior by setting TrackSelectionParameters with setTrackSelectionParameters.
  • Player instances can provide MediaMetadata about the currently playing item, which can be obtained by calling getMediaMetadata.
  • Player instances can provide information about ads in its media structure, for example via isPlayingAd.
  • Player instances can accept different types of video outputs, like SurfaceView or TextureView for video rendering.
  • Player instances can handle playback speed, audio attributes, and audio volume.
  • Player instances can provide information about the playback device, which may be remote, and allow to change the device's volume.

API stability guarantees

The majority of the Player interface and its related classes are part of the stable API that guarantees backwards-compatibility for users of the API. Only more advances use cases may need to rely on UnstableApi classes and methods that are subject to incompatible changes or even removal in a future release. Implementors of the Player interface are not covered by these API stability guarantees.

Player state

Users can listen to state changes by adding a Player.Listener with addListener.

The main elements of the overall player state are:

  • Playlist
  • Playback state
    • STATE_IDLE: This is the initial state, the state when the player is stopped, and when playback failed. The player will hold only limited resources in this state. prepare must be called to transition away from this state.
    • STATE_BUFFERING: The player is not able to immediately play from its current position. This mostly happens because more data needs to be loaded.
    • STATE_READY: The player is able to immediately play from its current position.
    • STATE_ENDED: The player finished playing all media, or there is no media to play.
  • Play/Pause, playback suppression and isPlaying
  • Playback position

Note that there are no callbacks for normal playback progression, only for transitions between media items and other position discontinuities. Code that needs to monitor playback progress (for example, an UI progress bar) should query the current position in appropriate intervals.

Implementing the Player interface

Implementing the Player interface is complex, as the interface includes many convenience methods that need to provide a consistent state and behavior, requires correct handling of listeners and available commands, and expects immediate state changes even if methods are internally handled asynchronously. For this reason, implementations are advised to inherit that handles all of these complexities and provides a simpler integration point for implementors of the interface.

Summary

Nested types

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.Command

Commands that indicate which method calls are currently permitted on a particular Player instance.

public final class Player.Commands

A set of commands.

A builder for Commands instances.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.DiscontinuityReason

Reasons for position discontinuities.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.Event

Events that can be reported via onEvents.

public final class Player.Events

A set of events.

public interface Player.Listener

Listener for changes in a Player.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.MediaItemTransitionReason

Reasons for media item transitions.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.PlayWhenReadyChangeReason

Reasons for playWhenReady changes.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.PlaybackSuppressionReason

Reason why playback is suppressed even though getPlayWhenReady is true.

public final class Player.PositionInfo

Position info describing a playback position involved in a discontinuity.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.RepeatMode

Repeat modes for playback.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.State

Playback state.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation Player.TimelineChangeReason

Reasons for timeline changes.

Constants

default static final int

This field is deprecated.

Use COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS instead.

default static final int

Command to increase and decrease the device volume and mute it with volume flags.

default static final int

Command to change the media items in the playlist.

default static final int

Command to get the player current AudioAttributes.

default static final int

Command to get information about the currently playing MediaItem.

default static final int

Command to get the device volume and whether it is muted.

default static final int

This field is deprecated.

Use COMMAND_GET_METADATA instead.

default static final int

Command to get metadata related to the playlist and current MediaItem.

default static final int

Command to get the text that should currently be displayed by the player.

default static final int

Command to get the information about the current timeline.

default static final int

Command to get details of the current track selection.

default static final int

Command to get the player volume.

default static final int

Represents an invalid Command.

default static final int

Command to start, pause or resume playback.

default static final int

Command to prepare the player.

default static final int

Command to release the player.

default static final int

Command to seek back by a fixed increment inside the current MediaItem.

default static final int

Command to seek forward by a fixed increment inside the current MediaItem.

default static final int

Command to seek anywhere inside the current MediaItem.

default static final int

This field is deprecated.

Use COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM instead.

default static final int

Command to seek to the default position of the current MediaItem.

default static final int

Command to seek anywhere in any MediaItem.

default static final int

Command to seek to a later position in the current MediaItem or the default position of the next MediaItem.

default static final int

Command to seek to the default position of the next MediaItem.

default static final int

This field is deprecated.

Use COMMAND_SEEK_TO_NEXT_MEDIA_ITEM instead.

default static final int

Command to seek to an earlier position in the current MediaItem or the default position of the previous MediaItem.

default static final int

Command to seek to the default position of the previous MediaItem.

default static final int

This field is deprecated.

Use COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM instead.

default static final int

This field is deprecated.

Use COMMAND_SEEK_TO_MEDIA_ITEM instead.

default static final int

Command to set the player's audio attributes.

default static final int

This field is deprecated.

Use COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS instead.

default static final int

Command to set the device volume with volume flags.

default static final int

Command to set a MediaItem.

default static final int

This field is deprecated.

Use COMMAND_SET_PLAYLIST_METADATA instead.

default static final int

Command to set the playlist metadata.

default static final int

Command to set the repeat mode.

default static final int

Command to enable shuffling.

default static final int

Command to set the playback speed and pitch.

default static final int

Command to set the player's track selection parameters.

default static final int

Command to set and clear the surface on which to render the video.

default static final int

Command to set the player volume.

default static final int

Command to stop playback.

default static final int

Automatic playback transition from one period in the timeline to the next.

default static final int

Discontinuity introduced internally (e.g. by the source).

default static final int

Discontinuity caused by the removal of the current period from the Timeline.

default static final int

Seek within the current period or to another period.

default static final int

Seek adjustment due to being unable to seek to the requested position or because the seek was permitted to be inexact.

default static final int

Discontinuity introduced by a skipped silence.

default static final int

Discontinuity introduced by a skipped period (for instance a skipped ad).

default static final int

getAudioAttributes changed.

default static final int

The audio session id was set.

default static final int

isCommandAvailable changed for at least one Command.

default static final int

getCurrentCues changed.

default static final int

getDeviceInfo changed.

default static final int

getDeviceVolume changed.

default static final int

isLoading ()} changed.

default static final int

isPlaying changed.

default static final int

getMaxSeekToPreviousPosition changed.

default static final int

getCurrentMediaItem changed or the player started repeating the current item.

default static final int

getMediaMetadata changed.

default static final int

Metadata associated with the current playback time changed.

default static final int

getPlaybackParameters changed.

default static final int

getPlaybackState changed.

default static final int

getPlaybackSuppressionReason changed.

default static final int

getPlayerError changed.

default static final int

getPlaylistMetadata changed.

default static final int

getPlayWhenReady changed.

default static final int

A position discontinuity occurred.

default static final int

A frame is rendered for the first time since setting the surface, or since the renderer was reset, or since the stream being rendered was changed.

default static final int

getRepeatMode changed.

default static final int

getSeekBackIncrement changed.

default static final int

getSeekForwardIncrement changed.

default static final int

getShuffleModeEnabled changed.

default static final int

Skipping silences in the audio stream is enabled or disabled.

default static final int

The size of the surface onto which the video is being rendered changed.

default static final int

getCurrentTimeline changed.

default static final int

getCurrentTracks changed.

default static final int

getTrackSelectionParameters changed.

default static final int

getVideoSize changed.

default static final int

getVolume changed.

default static final int

Playback has automatically transitioned to the next media item.

default static final int

The current media item has changed because of a change in the playlist.

default static final int

The media item has been repeated.

default static final int

A seek to another media item has occurred.

default static final int

Playback is not suppressed.

default static final int

Playback is suppressed due to transient audio focus loss.

default static final int

Playback is suppressed due to attempt to play on an unsuitable audio output (e.g. attempt to play on built-in speaker on a Wear OS device).

default static final int

This field is deprecated.

Use PLAYBACK_SUPPRESSION_REASON_UNSUITABLE_AUDIO_OUTPUT instead.

default static final int

Playback has been paused to avoid becoming noisy.

default static final int

Playback has been paused because of a loss of audio focus.

default static final int

Playback has been paused at the end of a media item.

default static final int

Playback has been started or paused because of a remote change.

default static final int

Playback has been paused because playback has been suppressed too long.

default static final int

Playback has been started or paused by a call to setPlayWhenReady.

default static final int

Repeats the entire timeline infinitely.

default static final int

Normal playback without repetition.

default static final int

Repeats the currently playing MediaItem infinitely during ongoing playback.

default static final int

The player is not able to immediately play the media, but is doing work toward being able to do so.

default static final int

The player has finished playing the media.

default static final int

The player is idle, meaning it holds only limited resources.

default static final int

The player is able to immediately play from its current position.

default static final int

Timeline changed as a result of a change of the playlist items or the order of the items.

default static final int

Timeline changed as a result of a source update (e.g. result of a dynamic update by the played media).

Public methods

abstract void

Registers a listener to receive all events from the player.

abstract void

Adds a media item to the end of the playlist.

abstract void
addMediaItem(int index, MediaItem mediaItem)

Adds a media item at the given index of the playlist.

abstract void

Adds a list of media items to the end of the playlist.

abstract void
addMediaItems(int index, List<MediaItem> mediaItems)

Adds a list of media items at the given index of the playlist.

abstract boolean

Returns whether the player can be used to advertise a media session.

abstract void

Clears the playlist.

abstract void

Clears any Surface, SurfaceHolder, SurfaceView or TextureView currently set on the player.

abstract void

Clears the Surface onto which video is being rendered if it matches the one passed.

abstract void

Clears the SurfaceHolder that holds the Surface onto which video is being rendered if it matches the one passed.

abstract void

Clears the SurfaceView onto which video is being rendered if it matches the one passed.

abstract void

Clears the TextureView onto which video is being rendered if it matches the one passed.

abstract void

This method is deprecated.

Use decreaseDeviceVolume instead.

abstract void

Decreases the volume of the device.

abstract Looper

Returns the Looper associated with the application thread that's used to access the player and on which player events are received.

abstract AudioAttributes

Returns the attributes for audio playback.

abstract Player.Commands

Returns the player's currently available Commands.

abstract @IntRange(from = 0, to = 100) int

Returns an estimate of the percentage in the current content or ad up to which data is buffered, or 0 if no estimate is available.

abstract long

Returns an estimate of the position in the current content or ad up to which data is buffered, in milliseconds.

abstract long

If isPlayingAd returns true, returns an estimate of the content position in the current content up to which data is buffered, in milliseconds.

abstract long

If isPlayingAd returns true, returns the duration of the current content in milliseconds, or TIME_UNSET if the duration is not known.

abstract long

If isPlayingAd returns true, returns the content position that will be played once all ads in the ad group have finished playing, in milliseconds.

abstract int

If isPlayingAd returns true, returns the index of the ad group in the period currently being played.

abstract int

If isPlayingAd returns true, returns the index of the ad in its ad group.

abstract CueGroup

Returns the current CueGroup.

abstract long

Returns the offset of the current playback position from the live edge in milliseconds, or TIME_UNSET if the current MediaItemisn't live or the offset is unknown.

abstract @Nullable Object

Returns the current manifest.

abstract @Nullable MediaItem

Returns the currently playing MediaItem.

abstract int

Returns the index of the current MediaItem in the timeline, or the prospective index if the current timeline is empty.

abstract int

Returns the index of the period currently being played.

abstract long

Returns the playback position in the current content or ad, in milliseconds, or the prospective position in milliseconds if the current timeline is empty.

abstract Timeline

Returns the current Timeline.

abstract Tracks

Returns the current tracks.

abstract int

This method is deprecated.

Use getCurrentMediaItemIndex instead.

abstract DeviceInfo

Gets the device information.

abstract @IntRange(from = 0) int

Gets the current volume of the device.

abstract long

Returns the duration of the current content or ad in milliseconds, or TIME_UNSET if the duration is not known.

abstract long

Returns the maximum position for which seekToPrevious seeks to the previous , in milliseconds.

abstract MediaItem
getMediaItemAt(int index)

Returns the MediaItem at the given index.

abstract int

Returns the number of media items in the playlist.

abstract MediaMetadata

Returns the current combined MediaMetadata, or EMPTY if not supported.

abstract int

Returns the index of the MediaItem that will be played if seekToNextMediaItem is called, which may depend on the current repeat mode and whether shuffle mode is enabled.

abstract int

This method is deprecated.

Use getNextMediaItemIndex instead.

abstract boolean

Whether playback will proceed when getPlaybackState == STATE_READY.

abstract PlaybackParameters

Returns the currently active playback parameters.

abstract int

Returns the current playback state of the player.

abstract int

Returns the reason why playback is suppressed even though getPlayWhenReady is true, or PLAYBACK_SUPPRESSION_REASON_NONE if playback is not suppressed.

abstract @Nullable PlaybackException

Returns the error that caused playback to fail.

abstract MediaMetadata

Returns the playlist MediaMetadata, as set by setPlaylistMetadata, or EMPTY if not supported.

abstract int

Returns the index of the MediaItem that will be played if seekToPreviousMediaItem is called, which may depend on the current repeat mode and whether shuffle mode is enabled.

abstract int

This method is deprecated.

Use getPreviousMediaItemIndex instead.

abstract int

Returns the current RepeatMode used for playback.

abstract long

Returns the seekBack increment.

abstract long

Returns the seekForward increment.

abstract boolean

Returns whether shuffling of media items is enabled.

abstract Size

Gets the size of the surface on which the video is rendered.

abstract long

Returns an estimate of the total buffered duration from the current position, in milliseconds.

abstract TrackSelectionParameters

Returns the parameters constraining the track selection.

abstract VideoSize

Gets the size of the video.

abstract @FloatRange(from = 0, to = 1.0) float

Returns the audio volume, with 0 being silence and 1 being unity gain (signal unchanged).

abstract boolean

This method is deprecated.

Use hasNextMediaItem instead.

abstract boolean

Returns whether a next MediaItem exists, which may depend on the current repeat mode and whether shuffle mode is enabled.

abstract boolean

This method is deprecated.

Use hasNextMediaItem instead.

abstract boolean

Returns whether a previous media item exists, which may depend on the current repeat mode and whether shuffle mode is enabled.

abstract void

This method is deprecated.

Use increaseDeviceVolume instead.

abstract void

Increases the volume of the device.

abstract boolean

Returns whether the provided Command is available.

abstract boolean

Returns whether the current MediaItem is dynamic (may change when the Timeline is updated), or false if the Timeline is empty.

abstract boolean

Returns whether the current MediaItem is live, or false if the Timeline is empty.

abstract boolean

Returns whether the current MediaItem is seekable, or false if the is empty.

abstract boolean

This method is deprecated.

Use isCurrentMediaItemDynamic instead.

abstract boolean

This method is deprecated.

Use isCurrentMediaItemLive instead.

abstract boolean

This method is deprecated.

Use isCurrentMediaItemSeekable instead.

abstract boolean

Gets whether the device is muted or not.

abstract boolean

Whether the player is currently loading the source.

abstract boolean

Returns whether the player is playing, i.e. getCurrentPosition is advancing.

abstract boolean

Returns whether the player is currently playing an ad.

abstract void
moveMediaItem(int currentIndex, int newIndex)

Moves the media item at the current index to the new index.

abstract void
moveMediaItems(int fromIndex, int toIndex, int newIndex)

Moves the media item range to the new index.

abstract void

This method is deprecated.

Use seekToNextMediaItem instead.

abstract void

Pauses playback.

abstract void

Resumes playback as soon as getPlaybackState == STATE_READY.

abstract void

Prepares the player.

abstract void

Releases the player.

abstract void

Unregister a listener registered through addListener.

abstract void
removeMediaItem(int index)

Removes the media item at the given index of the playlist.

abstract void
removeMediaItems(int fromIndex, int toIndex)

Removes a range of media items from the playlist.

abstract void
replaceMediaItem(int index, MediaItem mediaItem)

Replaces the media item at the given index of the playlist.

abstract void
replaceMediaItems(
    int fromIndex,
    int toIndex,
    List<MediaItem> mediaItems
)

Replaces the media items at the given range of the playlist.

abstract void

Seeks back in the current MediaItem by getSeekBackIncrement milliseconds.

abstract void

Seeks forward in the current MediaItem by getSeekForwardIncrement milliseconds.

abstract void
seekTo(long positionMs)

Seeks to a position specified in milliseconds in the current MediaItem.

abstract void
seekTo(int mediaItemIndex, long positionMs)

Seeks to a position specified in milliseconds in the specified MediaItem.

abstract void

Seeks to the default position associated with the current MediaItem.

abstract void
seekToDefaultPosition(int mediaItemIndex)

Seeks to the default position associated with the specified MediaItem.

abstract void

Seeks to a later position in the current or next MediaItem (if available).

abstract void

Seeks to the default position of the next MediaItem, which may depend on the current repeat mode and whether shuffle mode is enabled.

abstract void

This method is deprecated.

Use seekToNextMediaItem instead.

abstract void

Seeks to an earlier position in the current or previous MediaItem (if available).

abstract void

Seeks to the default position of the previous MediaItem, which may depend on the current repeat mode and whether shuffle mode is enabled.

abstract void

This method is deprecated.

Use seekToPreviousMediaItem instead.

abstract void
setAudioAttributes(
    AudioAttributes audioAttributes,
    boolean handleAudioFocus
)

Sets the attributes for audio playback, used by the underlying audio track.

abstract void
setDeviceMuted(boolean muted)

This method is deprecated.

Use setDeviceMuted instead.

abstract void
setDeviceMuted(boolean muted, @C.VolumeFlags int flags)

Sets the mute state of the device.

abstract void
setDeviceVolume(@IntRange(from = 0) int volume)

This method is deprecated.

Use setDeviceVolume instead.

abstract void
setDeviceVolume(@IntRange(from = 0) int volume, @C.VolumeFlags int flags)

Sets the volume of the device with volume flags.

abstract void

Clears the playlist, adds the specified MediaItem and resets the position to the default position.

abstract void
setMediaItem(MediaItem mediaItem, boolean resetPosition)

Clears the playlist and adds the specified MediaItem.

abstract void
setMediaItem(MediaItem mediaItem, long startPositionMs)

Clears the playlist and adds the specified MediaItem.

abstract void

Clears the playlist, adds the specified media items and resets the position to the default position.

abstract void
setMediaItems(List<MediaItem> mediaItems, boolean resetPosition)

Clears the playlist and adds the specified media items.

abstract void
setMediaItems(
    List<MediaItem> mediaItems,
    int startIndex,
    long startPositionMs
)

Clears the playlist and adds the specified media items.

abstract void
setPlayWhenReady(boolean playWhenReady)

Sets whether playback should proceed when getPlaybackState == STATE_READY.

abstract void

Attempts to set the playback parameters.

abstract void
setPlaybackSpeed(@FloatRange(from = 0, fromInclusive = false) float speed)

Changes the rate at which playback occurs.

abstract void

Sets the playlist MediaMetadata.

abstract void

Sets the RepeatMode to be used for playback.

abstract void
setShuffleModeEnabled(boolean shuffleModeEnabled)

Sets whether shuffling of media items is enabled.

abstract void