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