Player
interface Player
BasePlayer |
Abstract base |
ExoPlayer |
An extensible media player that plays |
ForwardingPlayer |
|
MediaController |
A controller that interacts with a |
CastPlayer |
|
CompositionPlayer |
A |
FakePlayer |
A fake |
ForwardingSimpleBasePlayer |
A |
MediaBrowser |
Browses media content offered by a |
RemoteCastPlayer |
|
SimpleBasePlayer |
A base implementation for |
SimpleExoPlayer |
This class is deprecated. Use |
StubExoPlayer |
An abstract |
StubPlayer |
An abstract |
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 threadunless 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 commandsto signal feature support and users of the interface must only call methods if the correspondingCommandis available. - Users can register
Player.Listenercallbacks 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
MediaIteminstances. The player can also supportrepeat modesand shuffling within this playlist. The player provides aTimelinerepresenting the structure of the playlist and all its items, which can be obtained by callinggetCurrentTimeline - Player instances can provide seeking within the currently playing item and to other items, using the various
seek...methods. - Player instances can provide
Tracksdefining the currently available and selected tracks, which can be obtained by callinggetCurrentTracks. Users can also modify track selection behavior by settingTrackSelectionParameterswithsetTrackSelectionParameters. - Player instances can provide
MediaMetadataabout the currently playing item, which can be obtained by callinggetMediaMetadata. - 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
SurfaceVieworTextureViewfor video rendering. - Player instances can handle
playback speed,audio attributes, andaudio 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
MediaIteminstances can be added with methods like setMediaItem to define what the player will be playing.- The current playlist can be obtained via
getCurrentTimelineand convenience methods likegetMediaItemCountorgetCurrentMediaItem. - With an empty playlist, the player can only be in
STATE_IDLEorSTATE_ENDED.
- Playback state
STATE_IDLE: This is the initial state, the state when the player isstopped, and when playbackfailed. The player will hold only limited resources in this state.preparemust 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
playWhenReady: Indicates the user intention to play. It can be set withplayorpause.playback suppression: Defines a reason for which playback will be suppressed even ifplayWhenReadyistrue.isPlaying: Whether the player is playing (that is, its position is advancing and media is being presented). This will only betrueif playback state isSTATE_READY,playWhenReadyistrue, and playback is not suppressed.
- Playback position
media item index: The index in the playlist.ad insertion: Whether an inserted ad is playing and whichad group indexandad index in the groupit belongs tocurrent position: The current position of the playback. This is the same as thecontent positionunless an ad is playing, where this indicates the position in the inserted ad.
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 SimpleBasePlayer that handles all of these complexities and provides a simpler integration point for implementors of the interface.
Summary
Nested types |
|---|
@DocumentedCommands that indicate which method calls are currently permitted on a particular |
class Player.CommandsA set of |
|
A builder for |
@DocumentedReasons for position discontinuities. |
@DocumentedEvents that can be reported via |
class Player.EventsA set of |
interface Player.ListenerListener for changes in a |
@DocumentedReasons for media item transitions. |
@DocumentedReasons for |
@DocumentedReason why playback is suppressed even though |
class Player.PositionInfoPosition info describing a playback position involved in a discontinuity. |
@DocumentedRepeat modes for playback. |
@DocumentedPlayback state. |
@DocumentedReasons for timeline changes. |
Constants |
|
|---|---|
const Int |
This property is deprecated. Use |
const Int |
Command to increase and decrease the device volume and mute it with volume flags. |
const Int |
Command to change the |
const Int |
Command to get the player current |
const Int |
Command to get information about the currently playing |
const Int |
Command to get the device volume and whether it is muted. |
const Int |
This property is deprecated. Use |
const Int |
COMMAND_GET_METADATA = 18Command to get metadata related to the playlist and current |
const Int |
COMMAND_GET_TEXT = 28Command to get the text that should currently be displayed by the player. |
const Int |
COMMAND_GET_TIMELINE = 17Command to get the information about the current timeline. |
const Int |
COMMAND_GET_TRACKS = 30Command to get details of the current track selection. |
const Int |
COMMAND_GET_VOLUME = 22Command to get the player volume. |
const Int |
COMMAND_INVALID = -1Represents an invalid |
const Int |
Command to start, pause or resume playback. |
const Int |
COMMAND_PREPARE = 2Command to prepare the player. |
const Int |
COMMAND_RELEASE = 32Command to release the player. |
const Int |
COMMAND_SEEK_BACK = 11Command to seek back by a fixed increment inside the current |
const Int |
COMMAND_SEEK_FORWARD = 12Command to seek forward by a fixed increment inside the current |
const Int |
Command to seek anywhere inside the current |
const Int |
This property is deprecated. Use |
const Int |
Command to seek to the default position of the current |
const Int |
Command to seek anywhere in any |
const Int |
Command to seek to a later position in the current |
const Int |
Command to seek to the default position of the next |
const Int |
This property is deprecated. Use |
const Int |
Command to seek to an earlier position in the current |
const Int |
Command to seek to the default position of the previous |
const Int |
This property is deprecated. Use |
const Int |
This property is deprecated. Use |
const Int |
Command to set the player's audio attributes. |
const Int |
This property is deprecated. Use |
const Int |
Command to set the device volume with volume flags. |
const Int |
Command to set a |
const Int |
This property is deprecated. Use |
const Int |
Command to set the playlist metadata. |
const Int |
Command to set the repeat mode. |
const Int |
Command to enable shuffling. |
const Int |
Command to set the playback speed and pitch. |
const Int |
Command to set the player's track selection parameters. |
const Int |
Command to set and clear the surface on which to render the video. |
const Int |
COMMAND_SET_VOLUME = 24Command to set the player volume. |
const Int |
COMMAND_STOP = 3Command to stop playback. |
const Int |
Automatic playback transition from one period in the timeline to the next. |
const Int |
Discontinuity introduced internally (e.g. by the source). |
const Int |
Discontinuity caused by the removal of the current period from the |
const Int |
Seek within the current period or to another period. |
const Int |
Seek adjustment due to being unable to seek to the requested position or because the seek was permitted to be inexact. |
const Int |
Discontinuity introduced by a skipped silence. |
const Int |
Discontinuity introduced by a skipped period (for instance a skipped ad). |
const Int |
|
const Int |
The audio session id was set. |
const Int |
|
const Int |
EVENT_CUES = 27
|
const Int |
|
const Int |
|
const Int |
|
const Int |
|
const Int |
|
const Int |
|
const Int |
|
const Int |
EVENT_METADATA = 28Metadata associated with the current playback time changed. |
const Int |
|
const Int |
|
const Int |
|
const Int |
EVENT_PLAYER_ERROR = 10
|
const Int |
|
const Int |
|
const Int |
A position discontinuity occurred. |
const 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. |
const Int |
|
const Int |
|
const Int |
|
const Int |
|
const Int |
Skipping silences in the audio stream is enabled or disabled. |
const Int |
The size of the surface onto which the video is being rendered changed. |
const Int |
|
const Int |
|
const Int |
|
const Int |
|
const Int |
EVENT_VOLUME_CHANGED = 22
|
const Int |
Playback has automatically transitioned to the next media item. |
const Int |
The current media item has changed because of a change in the playlist. |
const Int |
The media item has been repeated. |
const Int |
A seek to another media item has occurred. |
const Int |
Playback is not suppressed. |
const Int |
Playback is suppressed because the player is currently scrubbing. |
const Int |
Playback is suppressed due to transient audio focus loss. |
const 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). |
const Int |
This property is deprecated. Use |
const Int |
Playback has been paused to avoid becoming noisy. |
const Int |
Playback has been paused because of a loss of audio focus. |
const Int |
Playback has been paused at the end of a media item. |
const Int |
Playback has been started or paused because of a remote change. |
const Int |
Playback has been paused because playback has been |
const Int |
Playback has been started or paused by a call to |
const Int |
REPEAT_MODE_ALL = 2Repeats the entire timeline infinitely. |
const Int |
REPEAT_MODE_OFF = 0Normal playback without repetition. |
const Int |
REPEAT_MODE_ONE = 1Repeats the currently playing |
const Int |
STATE_BUFFERING = 2The player is not able to immediately play the media, but is doing work toward being able to do so. |
const Int |
STATE_ENDED = 4The player has finished playing the media. |
const Int |
STATE_IDLE = 1The player is idle, meaning it holds only limited resources. |
const Int |
STATE_READY = 3The player is able to immediately play from its current position. |
const Int |
Timeline changed as a result of a change of the playlist items or the order of the items. |
const Int |
Timeline changed as a result of a source update (e.g. result of a dynamic update by the played media). |
Public functions |
|
|---|---|
Unit |
addListener(listener: Player.Listener!)Registers a listener to receive all events from the player. |
Unit |
addMediaItem(mediaItem: MediaItem!)Adds a media item to the end of the playlist. |
Unit |
addMediaItem(index: Int, mediaItem: MediaItem!)Adds a media item at the given index of the playlist. |
Unit |
addMediaItems(mediaItems: (Mutable)List<MediaItem!>!)Adds a list of media items to the end of the playlist. |
Unit |
addMediaItems(index: Int, mediaItems: (Mutable)List<MediaItem!>!)Adds a list of media items at the given index of the playlist. |
Boolean |
Returns whether the player can be used to advertise a media session. |
Unit |
Clears the playlist. |
Unit |
Clears any |
Unit |
clearVideoSurface(surface: Surface?)Clears the |
Unit |
clearVideoSurfaceHolder(surfaceHolder: SurfaceHolder?)Clears the |
Unit |
clearVideoSurfaceView(surfaceView: SurfaceView?)Clears the |
Unit |
clearVideoTextureView(textureView: TextureView?)Clears the |
Unit |
This function is deprecated. Use |
Unit |
decreaseDeviceVolume(@C.VolumeFlags flags: Int)Decreases the volume of the device. |
Looper! |
Returns the |
AudioAttributes! |
Returns the attributes for audio playback. |
Int |
Returns the audio session identifier, or |
Player.Commands! |
Returns the player's currently available |
@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. |
Long |
Returns an estimate of the position in the current content or ad up to which data is buffered, in milliseconds. |
Long |
If |
Long |
If |
Long |
If |
Int |
If |
Int |
If |
CueGroup! |
Returns the current |
Long |
Returns the offset of the current playback position from the live edge in milliseconds, or |
Any? |
Returns the current manifest. |
MediaItem? |
Returns the currently playing |
Int |
Returns the index of the current |
Int |
Returns the index of the period currently being played. |
Long |
Returns the playback position in the current content or ad, in milliseconds, or the prospective position in milliseconds if the |
Timeline! |
Returns the current |
Tracks! |
Returns the current tracks. |
Int |
This function is deprecated. Use |
DeviceInfo! |
Gets the device information. |
@IntRange(from = 0) Int |
Gets the current volume of the device. |
Long |
Returns the duration of the current content or ad in milliseconds, or |
Long |
Returns the maximum position for which |
MediaItem! |
getMediaItemAt(index: Int)Returns the |
Int |
Returns the number of |
MediaMetadata! |
Returns the current combined |
Int |
Returns the index of the |
Int |
This function is deprecated. Use |
Boolean |
Whether playback will proceed when |
PlaybackParameters! |
Returns the currently active playback parameters. |
Int |
Returns the current |
Int |
Returns the reason why playback is suppressed even though |
PlaybackException? |
Returns the error that caused playback to fail. |
MediaMetadata! |
Returns the playlist |
Int |
Returns the index of the |
Int |
This function is deprecated. Use |
Int |
Returns the current |
Long |
Returns the |
Long |
Returns the |
Boolean |
Returns whether shuffling of media items is enabled. |
Size! |
Gets the size of the surface on which the video is rendered. |
Long |
Returns an estimate of the total buffered duration from the current position, in milliseconds. |
TrackSelectionParameters! |
Returns the parameters constraining the track selection. |
VideoSize! |
Gets the size of the video. |
@FloatRange(from = 0, to = 1.0) Float |
Returns the audio volume, with 0 being silence and 1 being unity gain (signal unchanged). |
Boolean |
Returns whether a next |
Boolean |
Returns whether a previous media item exists, which may depend on the current repeat mode and whether shuffle mode is enabled. |
Unit |
This function is deprecated. Use |
Unit |
increaseDeviceVolume(@C.VolumeFlags flags: Int)Increases the volume of the device. |
Boolean |
isCommandAvailable(@Player.Command command: Int)Returns whether the provided |
Boolean |
Returns whether the current |
Boolean |
Returns whether the current |
Boolean |
Returns whether the current |
Boolean |
This function is deprecated. Use |
Boolean |
This function is deprecated. Use |
Boolean |
This function is deprecated. Use |
Boolean |
Gets whether the device is muted or not. |
Boolean |
Whether the player is currently loading the source. |
Boolean |
Returns whether the player is playing, i.e. |
Boolean |
Returns whether the player is currently playing an ad. |
Unit |
moveMediaItem(currentIndex: Int, newIndex: Int)Moves the media item at the current index to the new index. |
Unit |
moveMediaItems(fromIndex: Int, toIndex: Int, newIndex: Int)Moves the media item range to the new index. |
Unit |
Sets the audio volume to 0. |
Unit |
pause()Pauses playback. |
Unit |
play()Resumes playback as soon as |
Unit |
prepare()Prepares the player. |
Unit |
release()Releases the player. |
Unit |
removeListener(listener: Player.Listener!)Unregister a listener registered through |
Unit |
removeMediaItem(index: Int)Removes the media item at the given index of the playlist. |
Unit |
removeMediaItems(fromIndex: Int, toIndex: Int)Removes a range of media items from the playlist. |
Unit |
replaceMediaItem(index: Int, mediaItem: MediaItem!)Replaces the media item at the given index of the playlist. |
Unit |
Replaces the media items at the given range of the playlist. |
Unit |
seekBack()Seeks back in the current |
Unit |
Seeks forward in the current |
Unit |
Seeks to a position specified in milliseconds in the current |
Unit |
Seeks to a position specified in milliseconds in the specified |
Unit |
Seeks to the default position associated with the current |
Unit |
seekToDefaultPosition(mediaItemIndex: Int)Seeks to the default position associated with the specified |
Unit |
Seeks to a later position in the current or next |
Unit |
Seeks to the default position of the next |
Unit |
Seeks to an earlier position in the current or previous |
Unit |
Seeks to the default position of the previous |
Unit |
setAudioAttributes(Sets the attributes for audio playback, used by the underlying audio track. |
Unit |
This function is deprecated. Use |
Unit |
setDeviceMuted(muted: Boolean, @C.VolumeFlags flags: Int)Sets the mute state of the device. |
Unit |
This function is deprecated. Use |
Unit |
setDeviceVolume(volume: @IntRange(from = 0) Int, @C.VolumeFlags flags: Int)Sets the volume of the device with volume flags. |
Unit |
setMediaItem(mediaItem: MediaItem!)Clears the playlist, adds the specified |
Unit |
setMediaItem(mediaItem: MediaItem!, resetPosition: Boolean)Clears the playlist and adds the specified |
Unit |
setMediaItem(mediaItem: MediaItem!, startPositionMs: Long)Clears the playlist and adds the specified |
Unit |
setMediaItems(mediaItems: (Mutable)List<MediaItem!>!)Clears the playlist, adds the specified |
Unit |
setMediaItems(Clears the playlist and adds the specified |
Unit |
Clears the playlist and adds the specified |
Unit |
setPlayWhenReady(playWhenReady: Boolean)Sets whether playback should proceed when |
Unit |
setPlaybackParameters(playbackParameters: PlaybackParameters!)Attempts to set the playback parameters. |
Unit |
setPlaybackSpeed(speed: @FloatRange(from = 0, fromInclusive = false) Float)Changes the rate at which playback occurs. |
Unit |
setPlaylistMetadata(mediaMetadata: MediaMetadata!)Sets the playlist |
Unit |
setRepeatMode(@Player.RepeatMode repeatMode: Int)Sets the |
Unit |
setShuffleModeEnabled(shuffleModeEnabled: Boolean)Sets whether shuffling of media items is enabled. |
Unit |
setTrackSelectionParameters(parameters: TrackSelectionParameters!)Sets the parameters constraining the track selection. |
Unit |
setVideoSurface(surface: Surface?)Sets the |
Unit |
setVideoSurfaceHolder(surfaceHolder: SurfaceHolder?)Sets the |
Unit |
setVideoSurfaceView(surfaceView: SurfaceView?)Sets the |
Unit |
setVideoTextureView(textureView: TextureView?)Sets the |
Unit |
setVolume(volume: @FloatRange(from = 0, to = 1.0) Float)Sets the audio volume, valid values are between 0 (silence) and 1 (unity gain, signal unchanged), inclusive. |
Unit |
stop()Stops playback without resetting the playlist. |
Unit |
If the audio volume is 0, sets the audio volume to a non-zero value decided by the Player to be the most appropriate. |
Extension functions |
|
|---|---|
suspend Nothing |
@UnstableApiContinuously listens to the |
suspend Nothing |
@UnstableApiContinuously listens to the |
PlayerStateObserver |
@UnstableApiUtility to observe |
Constants
COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS
const val COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS = 34: Int
Command to increase and decrease the device volume and mute it with volume flags.
The following methods must only be called if this command is available:
COMMAND_CHANGE_MEDIA_ITEMS
const val COMMAND_CHANGE_MEDIA_ITEMS = 20: Int
Command to change the media items in the playlist.
The following methods must only be called if this command is available:
COMMAND_GET_AUDIO_ATTRIBUTES
const val COMMAND_GET_AUDIO_ATTRIBUTES = 21: Int
Command to get the player current AudioAttributes.
The getAudioAttributes method must only be called if this command is available.
COMMAND_GET_CURRENT_MEDIA_ITEM
const val COMMAND_GET_CURRENT_MEDIA_ITEM = 16: Int
Command to get information about the currently playing MediaItem.
The following methods must only be called if this command is available:
getCurrentMediaItemisCurrentMediaItemDynamicisCurrentMediaItemLiveisCurrentMediaItemSeekablegetCurrentLiveOffsetgetDurationgetCurrentPositiongetBufferedPositiongetContentDurationgetContentPositiongetContentBufferedPositiongetTotalBufferedDurationisPlayingAdgetCurrentAdGroupIndexgetCurrentAdIndexInAdGroup
COMMAND_GET_DEVICE_VOLUME
const val COMMAND_GET_DEVICE_VOLUME = 23: Int
Command to get the device volume and whether it is muted.
The following methods must only be called if this command is available:
COMMAND_GET_METADATA
const val COMMAND_GET_METADATA = 18: Int
Command to get metadata related to the playlist and current MediaItem.
The following methods must only be called if this command is available:
COMMAND_GET_TEXT
const val COMMAND_GET_TEXT = 28: Int
Command to get the text that should currently be displayed by the player.
The getCurrentCues method must only be called if this command is available.
COMMAND_GET_TIMELINE
const val COMMAND_GET_TIMELINE = 17: Int
Command to get the information about the current timeline.
The following methods must only be called if this command is available:
COMMAND_GET_TRACKS
const val COMMAND_GET_TRACKS = 30: Int
Command to get details of the current track selection.
The getCurrentTracks method must only be called if this command is available.
COMMAND_GET_VOLUME
const val COMMAND_GET_VOLUME = 22: Int
Command to get the player volume.
The getVolume method must only be called if this command is available.
COMMAND_PLAY_PAUSE
const val COMMAND_PLAY_PAUSE = 1: Int
Command to start, pause or resume playback.
The following methods must only be called if this command is available:
COMMAND_PREPARE
const val COMMAND_PREPARE = 2: Int
Command to prepare the player.
The prepare method must only be called if this command is available.
COMMAND_RELEASE
const val COMMAND_RELEASE = 32: Int
Command to release the player.
The release method must only be called if this command is available.
COMMAND_SEEK_BACK
const val COMMAND_SEEK_BACK = 11: Int
Command to seek back by a fixed increment inside the current MediaItem.
The seekBack method must only be called if this command is available.
COMMAND_SEEK_FORWARD
const val COMMAND_SEEK_FORWARD = 12: Int
Command to seek forward by a fixed increment inside the current MediaItem.
The seekForward method must only be called if this command is available.
COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM
const val COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM = 5: Int
Command to seek anywhere inside the current MediaItem.
The seekTo method must only be called if this command is available.
COMMAND_SEEK_TO_DEFAULT_POSITION
const val COMMAND_SEEK_TO_DEFAULT_POSITION = 4: Int
Command to seek to the default position of the current MediaItem.
The seekToDefaultPosition method must only be called if this command is available.
COMMAND_SEEK_TO_MEDIA_ITEM
const val COMMAND_SEEK_TO_MEDIA_ITEM = 10: Int
Command to seek anywhere in any MediaItem.
The following methods must only be called if this command is available:
COMMAND_SEEK_TO_NEXT
const val COMMAND_SEEK_TO_NEXT = 9: Int
Command to seek to a later position in the current MediaItem or the default position of the next MediaItem.
The seekToNext method must only be called if this command is available.
COMMAND_SEEK_TO_NEXT_MEDIA_ITEM
const val COMMAND_SEEK_TO_NEXT_MEDIA_ITEM = 8: Int
Command to seek to the default position of the next MediaItem.
The seekToNextMediaItem method must only be called if this command is available.
COMMAND_SEEK_TO_PREVIOUS
const val COMMAND_SEEK_TO_PREVIOUS = 7: Int
Command to seek to an earlier position in the current MediaItem or the default position of the previous MediaItem.
The seekToPrevious method must only be called if this command is available.
COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM
const val COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM = 6: Int
Command to seek to the default position of the previous MediaItem.
The seekToPreviousMediaItem method must only be called if this command is available.
COMMAND_SET_AUDIO_ATTRIBUTES
const val COMMAND_SET_AUDIO_ATTRIBUTES = 35: Int
Command to set the player's audio attributes.
The setAudioAttributes method must only be called if this command is available.
COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS
const val COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS = 33: Int
Command to set the device volume with volume flags.
The setDeviceVolume method must only be called if this command is available.
COMMAND_SET_MEDIA_ITEM
const val COMMAND_SET_MEDIA_ITEM = 31: Int
Command to set a MediaItem.
The following methods must only be called if this command is available:
COMMAND_SET_PLAYLIST_METADATA
const val COMMAND_SET_PLAYLIST_METADATA = 19: Int
Command to set the playlist metadata.
The setPlaylistMetadata method must only be called if this command is available.
COMMAND_SET_REPEAT_MODE
const val COMMAND_SET_REPEAT_MODE = 15: Int
Command to set the repeat mode.
The setRepeatMode method must only be called if this command is available.
COMMAND_SET_SHUFFLE_MODE
const val COMMAND_SET_SHUFFLE_MODE = 14: Int
Command to enable shuffling.
The setShuffleModeEnabled method must only be called if this command is available.
COMMAND_SET_SPEED_AND_PITCH
const val COMMAND_SET_SPEED_AND_PITCH = 13: Int
Command to set the playback speed and pitch.
The following methods must only be called if this command is available:
COMMAND_SET_TRACK_SELECTION_PARAMETERS
const val COMMAND_SET_TRACK_SELECTION_PARAMETERS = 29: Int
Command to set the player's track selection parameters.
The setTrackSelectionParameters method must only be called if this command is available.
COMMAND_SET_VIDEO_SURFACE
const val COMMAND_SET_VIDEO_SURFACE = 27: Int
Command to set and clear the surface on which to render the video.
The following methods must only be called if this command is available:
COMMAND_SET_VOLUME
const val COMMAND_SET_VOLUME = 24: Int
Command to set the player volume.
The setVolume method must only be called if this command is available.
COMMAND_STOP
const val COMMAND_STOP = 3: Int
Command to stop playback.
The stop method must only be called if this command is available.
DISCONTINUITY_REASON_AUTO_TRANSITION
const val DISCONTINUITY_REASON_AUTO_TRANSITION = 0: Int
Automatic playback transition from one period in the timeline to the next. The period index may be the same as it was before the discontinuity in case the current period is repeated.
This reason also indicates an automatic transition from the content period to an inserted ad period or vice versa. Or a transition caused by another player (e.g. multiple controllers can control the same playback on a remote device).
DISCONTINUITY_REASON_INTERNAL
const val DISCONTINUITY_REASON_INTERNAL = 5: Int
Discontinuity introduced internally (e.g. by the source).
DISCONTINUITY_REASON_REMOVE
const val DISCONTINUITY_REASON_REMOVE = 4: Int
Discontinuity caused by the removal of the current period from the Timeline.
DISCONTINUITY_REASON_SEEK
const val DISCONTINUITY_REASON_SEEK = 1: Int
Seek within the current period or to another period.
DISCONTINUITY_REASON_SEEK_ADJUSTMENT
const val DISCONTINUITY_REASON_SEEK_ADJUSTMENT = 2: Int
Seek adjustment due to being unable to seek to the requested position or because the seek was permitted to be inexact.
DISCONTINUITY_REASON_SILENCE_SKIP
const val DISCONTINUITY_REASON_SILENCE_SKIP = 6: Int
Discontinuity introduced by a skipped silence.
DISCONTINUITY_REASON_SKIP
const val DISCONTINUITY_REASON_SKIP = 3: Int
Discontinuity introduced by a skipped period (for instance a skipped ad).
EVENT_AUDIO_ATTRIBUTES_CHANGED
const val EVENT_AUDIO_ATTRIBUTES_CHANGED = 20: Int
getAudioAttributes changed.
EVENT_AVAILABLE_COMMANDS_CHANGED
const val EVENT_AVAILABLE_COMMANDS_CHANGED = 13: Int
isCommandAvailable changed for at least one Command.
EVENT_DEVICE_VOLUME_CHANGED
const val EVENT_DEVICE_VOLUME_CHANGED = 30: Int
getDeviceVolume or isDeviceMuted changed.
EVENT_MAX_SEEK_TO_PREVIOUS_POSITION_CHANGED
const val EVENT_MAX_SEEK_TO_PREVIOUS_POSITION_CHANGED = 18: Int
getMaxSeekToPreviousPosition changed.
EVENT_MEDIA_ITEM_TRANSITION
const val EVENT_MEDIA_ITEM_TRANSITION = 1: Int
getCurrentMediaItem changed or the player started repeating the current item.
EVENT_MEDIA_METADATA_CHANGED
const val EVENT_MEDIA_METADATA_CHANGED = 14: Int
getMediaMetadata changed.
EVENT_METADATA
const val EVENT_METADATA = 28: Int
Metadata associated with the current playback time changed.
EVENT_PLAYBACK_PARAMETERS_CHANGED
const val EVENT_PLAYBACK_PARAMETERS_CHANGED = 12: Int
getPlaybackParameters changed.
EVENT_PLAYBACK_STATE_CHANGED
const val EVENT_PLAYBACK_STATE_CHANGED = 4: Int
getPlaybackState changed.
EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED
const val EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED = 6: Int
getPlaybackSuppressionReason changed.
EVENT_PLAYLIST_METADATA_CHANGED
const val EVENT_PLAYLIST_METADATA_CHANGED = 15: Int
getPlaylistMetadata changed.
EVENT_PLAY_WHEN_READY_CHANGED
const val EVENT_PLAY_WHEN_READY_CHANGED = 5: Int
getPlayWhenReady changed.
EVENT_POSITION_DISCONTINUITY
const val EVENT_POSITION_DISCONTINUITY = 11: Int
A position discontinuity occurred. See onPositionDiscontinuity.
EVENT_RENDERED_FIRST_FRAME
const val EVENT_RENDERED_FIRST_FRAME = 26: 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.
EVENT_SEEK_BACK_INCREMENT_CHANGED
const val EVENT_SEEK_BACK_INCREMENT_CHANGED = 16: Int
getSeekBackIncrement changed.
EVENT_SEEK_FORWARD_INCREMENT_CHANGED
const val EVENT_SEEK_FORWARD_INCREMENT_CHANGED = 17: Int
getSeekForwardIncrement changed.
EVENT_SHUFFLE_MODE_ENABLED_CHANGED
const val EVENT_SHUFFLE_MODE_ENABLED_CHANGED = 9: Int
getShuffleModeEnabled changed.
EVENT_SKIP_SILENCE_ENABLED_CHANGED
const val EVENT_SKIP_SILENCE_ENABLED_CHANGED = 23: Int
Skipping silences in the audio stream is enabled or disabled.
EVENT_SURFACE_SIZE_CHANGED
const val EVENT_SURFACE_SIZE_CHANGED = 24: Int
The size of the surface onto which the video is being rendered changed.
EVENT_TRACK_SELECTION_PARAMETERS_CHANGED
const val EVENT_TRACK_SELECTION_PARAMETERS_CHANGED = 19: Int
getTrackSelectionParameters changed.
MEDIA_ITEM_TRANSITION_REASON_AUTO
const val MEDIA_ITEM_TRANSITION_REASON_AUTO = 1: Int
Playback has automatically transitioned to the next media item.
This reason also indicates a transition caused by another player (e.g. multiple controllers can control the same playback on a remote device).
MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED
const val MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED = 3: Int
The current media item has changed because of a change in the playlist. This can either be if the media item previously being played has been removed, or when the playlist becomes non-empty after being empty.
MEDIA_ITEM_TRANSITION_REASON_REPEAT
const val MEDIA_ITEM_TRANSITION_REASON_REPEAT = 0: Int
The media item has been repeated.
MEDIA_ITEM_TRANSITION_REASON_SEEK
const val MEDIA_ITEM_TRANSITION_REASON_SEEK = 2: Int
A seek to another media item has occurred.
PLAYBACK_SUPPRESSION_REASON_NONE
const val PLAYBACK_SUPPRESSION_REASON_NONE = 0: Int
Playback is not suppressed.
PLAYBACK_SUPPRESSION_REASON_SCRUBBING
const val PLAYBACK_SUPPRESSION_REASON_SCRUBBING = 4: Int
Playback is suppressed because the player is currently scrubbing.
PLAYBACK_SUPPRESSION_REASON_TRANSIENT_AUDIO_FOCUS_LOSS
const val PLAYBACK_SUPPRESSION_REASON_TRANSIENT_AUDIO_FOCUS_LOSS = 1: Int
Playback is suppressed due to transient audio focus loss.
PLAYBACK_SUPPRESSION_REASON_UNSUITABLE_AUDIO_OUTPUT
const val PLAYBACK_SUPPRESSION_REASON_UNSUITABLE_AUDIO_OUTPUT = 3: 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).
PLAYBACK_SUPPRESSION_REASON_UNSUITABLE_AUDIO_ROUTE
const val PLAYBACK_SUPPRESSION_REASON_UNSUITABLE_AUDIO_ROUTE = 2: Int
PLAY_WHEN_READY_CHANGE_REASON_AUDIO_BECOMING_NOISY
const val PLAY_WHEN_READY_CHANGE_REASON_AUDIO_BECOMING_NOISY = 3: Int
Playback has been paused to avoid becoming noisy.
PLAY_WHEN_READY_CHANGE_REASON_AUDIO_FOCUS_LOSS
const val PLAY_WHEN_READY_CHANGE_REASON_AUDIO_FOCUS_LOSS = 2: Int
Playback has been paused because of a loss of audio focus.
PLAY_WHEN_READY_CHANGE_REASON_END_OF_MEDIA_ITEM
const val PLAY_WHEN_READY_CHANGE_REASON_END_OF_MEDIA_ITEM = 5: Int
Playback has been paused at the end of a media item.
PLAY_WHEN_READY_CHANGE_REASON_REMOTE
const val PLAY_WHEN_READY_CHANGE_REASON_REMOTE = 4: Int
Playback has been started or paused because of a remote change.
PLAY_WHEN_READY_CHANGE_REASON_SUPPRESSED_TOO_LONG
const val PLAY_WHEN_READY_CHANGE_REASON_SUPPRESSED_TOO_LONG = 6: Int
Playback has been paused because playback has been suppressed too long.
PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST
const val PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST = 1: Int
Playback has been started or paused by a call to setPlayWhenReady.
REPEAT_MODE_ALL
const val REPEAT_MODE_ALL = 2: Int
Repeats the entire timeline infinitely. "Previous" and "Next" actions behave as they do in REPEAT_MODE_OFF, but with looping at the ends so that "Previous" when playing the first MediaItem will move to the last MediaItem, and "Next" when playing the last MediaItem will move to the first MediaItem.
REPEAT_MODE_OFF
const val REPEAT_MODE_OFF = 0: Int
Normal playback without repetition. "Previous" and "Next" actions move to the previous and next MediaItem respectively, and do nothing when there is no previous or next MediaItem to move to.
REPEAT_MODE_ONE
const val REPEAT_MODE_ONE = 1: Int
Repeats the currently playing MediaItem infinitely during ongoing playback. "Previous" and "Next" actions behave as they do in REPEAT_MODE_OFF, moving to the previous and next MediaItem respectively, and doing nothing when there is no previous or next MediaItem to move to.
STATE_BUFFERING
const val STATE_BUFFERING = 2: Int
The player is not able to immediately play the media, but is doing work toward being able to do so. This state typically occurs when the player needs to buffer more data before playback can start.
STATE_IDLE
const val STATE_IDLE = 1: Int
The player is idle, meaning it holds only limited resources. The player must be prepared before it will play the media.
STATE_READY
const val STATE_READY = 3: Int
The player is able to immediately play from its current position. The player will be playing if getPlayWhenReady is true, and paused otherwise.
TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED
const val TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED = 0: Int
Timeline changed as a result of a change of the playlist items or the order of the items.
TIMELINE_CHANGE_REASON_SOURCE_UPDATE
const val TIMELINE_CHANGE_REASON_SOURCE_UPDATE = 1: Int
Timeline changed as a result of a source update (e.g. result of a dynamic update by the played media).
This reason also indicates a change caused by another player (e.g. multiple controllers can control the same playback on the remote device).
Public functions
addListener
fun addListener(listener: Player.Listener!): Unit
Registers a listener to receive all events from the player.
The listener's methods will be called on the thread associated with getApplicationLooper.
This method can be called from any thread.
| Parameters | |
|---|---|
listener: Player.Listener! |
The listener to register. |
addMediaItem
fun addMediaItem(mediaItem: MediaItem!): Unit
Adds a media item to the end of the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
addMediaItem
fun addMediaItem(index: Int, mediaItem: MediaItem!): Unit
Adds a media item at the given index of the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
addMediaItems
fun addMediaItems(mediaItems: (Mutable)List<MediaItem!>!): Unit
Adds a list of media items to the end of the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
mediaItems: (Mutable)List<MediaItem!>! |
The |
addMediaItems
fun addMediaItems(index: Int, mediaItems: (Mutable)List<MediaItem!>!): Unit
Adds a list of media items at the given index of the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
index: Int |
The index at which to add the media items. If the index is larger than the size of the playlist, the media items are added to the end of the playlist. |
mediaItems: (Mutable)List<MediaItem!>! |
The |
canAdvertiseSession
fun canAdvertiseSession(): Boolean
Returns whether the player can be used to advertise a media session.
clearMediaItems
fun clearMediaItems(): Unit
Clears the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
clearVideoSurface
fun clearVideoSurface(): Unit
Clears any Surface, SurfaceHolder, SurfaceView or TextureView currently set on the player.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
clearVideoSurface
fun clearVideoSurface(surface: Surface?): Unit
Clears the Surface onto which video is being rendered if it matches the one passed. Else does nothing.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
surface: Surface? |
The surface to clear. |
clearVideoSurfaceHolder
fun clearVideoSurfaceHolder(surfaceHolder: SurfaceHolder?): Unit
Clears the SurfaceHolder that holds the Surface onto which video is being rendered if it matches the one passed. Else does nothing.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
surfaceHolder: SurfaceHolder? |
The surface holder to clear. |
clearVideoSurfaceView
fun clearVideoSurfaceView(surfaceView: SurfaceView?): Unit
Clears the SurfaceView onto which video is being rendered if it matches the one passed. Else does nothing.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
surfaceView: SurfaceView? |
The texture view to clear. |
clearVideoTextureView
fun clearVideoTextureView(textureView: TextureView?): Unit
Clears the TextureView onto which video is being rendered if it matches the one passed. Else does nothing.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
textureView: TextureView? |
The texture view to clear. |
decreaseDeviceVolume
fun decreaseDeviceVolume(@C.VolumeFlags flags: Int): Unit
Decreases the volume of the device.
The getDeviceVolume device volume cannot be decreased below minVolume.
Note that this method affects the device volume. To change the volume of the current stream only, use setVolume.
This method must only be called if COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
@C.VolumeFlags flags: Int |
Either 0 or a bitwise combination of one or more |
getApplicationLooper
fun getApplicationLooper(): Looper!
Returns the Looper associated with the application thread that's used to access the player and on which player events are received.
This method can be called from any thread.
getAudioAttributes
fun getAudioAttributes(): AudioAttributes!
Returns the attributes for audio playback.
This method must only be called if COMMAND_GET_AUDIO_ATTRIBUTES is available.
| See also | |
|---|---|
onAudioAttributesChanged |
getAudioSessionId
@UnstableApi
fun getAudioSessionId(): Int
Returns the audio session identifier, or AUDIO_SESSION_ID_UNSET if not set.
| See also | |
|---|---|
onAudioSessionIdChanged |
getAvailableCommands
fun getAvailableCommands(): Player.Commands!
Returns the player's currently available Commands.
The returned Commands are not updated when available commands change. Use onAvailableCommandsChanged to get an update when the available commands change.
| Returns | |
|---|---|
Player.Commands! |
The currently available |
| See also | |
|---|---|
onAvailableCommandsChanged |
getBufferedPercentage
fun getBufferedPercentage(): @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.
getBufferedPosition
fun getBufferedPosition(): Long
Returns an estimate of the position in the current content or ad up to which data is buffered, in milliseconds.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getContentBufferedPosition
fun getContentBufferedPosition(): Long
If isPlayingAd returns true, returns an estimate of the content position in the current content up to which data is buffered, in milliseconds. If there is no ad playing, the returned position is the same as that returned by getBufferedPosition.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getContentDuration
fun getContentDuration(): Long
If isPlayingAd returns true, returns the duration of the current content in milliseconds, or TIME_UNSET if the duration is not known. If there is no ad playing, the returned duration is the same as that returned by getDuration.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getContentPosition
fun getContentPosition(): 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. If there is no ad playing, the returned position is the same as that returned by getCurrentPosition.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentAdGroupIndex
fun getCurrentAdGroupIndex(): Int
If isPlayingAd returns true, returns the index of the ad group in the period currently being played. Returns INDEX_UNSET otherwise.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentAdIndexInAdGroup
fun getCurrentAdIndexInAdGroup(): Int
If isPlayingAd returns true, returns the index of the ad in its ad group. Returns INDEX_UNSET otherwise.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentCues
fun getCurrentCues(): CueGroup!
Returns the current CueGroup.
This method must only be called if COMMAND_GET_TEXT is available.
| See also | |
|---|---|
onCues |
getCurrentLiveOffset
fun getCurrentLiveOffset(): 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.
The offset is calculated as currentTime - playbackPosition, so should usually be positive.
Note that this offset may rely on an accurate local time, so this method may return an incorrect value if the difference between system clock and server clock is unknown.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentManifest
@UnstableApi
fun getCurrentManifest(): Any?
Returns the current manifest. The type depends on the type of media being played. May be null.
getCurrentMediaItem
fun getCurrentMediaItem(): MediaItem?
Returns the currently playing MediaItem. May be null if the timeline is empty.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
| See also | |
|---|---|
onMediaItemTransition |
getCurrentMediaItemIndex
fun getCurrentMediaItemIndex(): Int
Returns the index of the current MediaItem in the timeline, or the prospective index if the current timeline is empty.
This method must only be called if COMMAND_GET_TIMELINE is available.
getCurrentPeriodIndex
fun getCurrentPeriodIndex(): Int
Returns the index of the period currently being played.
This method must only be called if COMMAND_GET_TIMELINE is available.
getCurrentPosition
fun getCurrentPosition(): 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.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentTimeline
fun getCurrentTimeline(): Timeline!
Returns the current Timeline. Never null, but may be empty.
This method must only be called if COMMAND_GET_TIMELINE is available.
| See also | |
|---|---|
onTimelineChanged |
getCurrentTracks
fun getCurrentTracks(): Tracks!
Returns the current tracks.
This method must only be called if COMMAND_GET_TRACKS is available.
| See also | |
|---|---|
onTracksChanged |
getDeviceVolume
fun getDeviceVolume(): @IntRange(from = 0) Int
Gets the current volume of the device.
For devices with local playback, the volume returned by this method varies according to the current stream type. The stream type is determined by getStreamType.
For devices with remote playback, the volume of the remote device is returned.
Note that this method returns the volume of the device. To check the current stream volume, use getVolume.
This method must only be called if COMMAND_GET_DEVICE_VOLUME is available.
| See also | |
|---|---|
onDeviceVolumeChanged |
getDuration
fun getDuration(): Long
Returns the duration of the current content or ad in milliseconds, or TIME_UNSET if the duration is not known.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getMaxSeekToPreviousPosition
fun getMaxSeekToPreviousPosition(): Long
Returns the maximum position for which seekToPrevious seeks to the previous MediaItem, in milliseconds.
| Returns | |
|---|---|
Long |
The maximum seek to previous position, in milliseconds. |
| See also | |
|---|---|
onMaxSeekToPreviousPositionChanged |
getMediaItemAt
fun getMediaItemAt(index: Int): MediaItem!
Returns the MediaItem at the given index.
This method must only be called if COMMAND_GET_TIMELINE is available.
getMediaItemCount
fun getMediaItemCount(): Int
Returns the number of media items in the playlist.
This method must only be called if COMMAND_GET_TIMELINE is available.
getMediaMetadata
fun getMediaMetadata(): MediaMetadata!
Returns the current combined MediaMetadata, or EMPTY if not supported.
This MediaMetadata is a combination of the MediaItem metadata, the static metadata in the media's Format, and any timed metadata that has been parsed from the media and output via onMetadata. If a field is populated in the mediaMetadata, it will be prioritised above the same field coming from static or timed metadata.
This method must only be called if COMMAND_GET_METADATA is available.
| See also | |
|---|---|
onMediaMetadataChanged |
getNextMediaItemIndex
fun getNextMediaItemIndex(): 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. Returns INDEX_UNSET if hasNextMediaItem is
false.
Note: When the repeat mode is REPEAT_MODE_ONE, this method behaves the same as when the current repeat mode is REPEAT_MODE_OFF. See REPEAT_MODE_ONE for more details.
This method must only be called if COMMAND_GET_TIMELINE is available.
getPlayWhenReady
fun getPlayWhenReady(): Boolean
Whether playback will proceed when getPlaybackState == STATE_READY.
| Returns | |
|---|---|
Boolean |
Whether playback will proceed when ready. |
| See also | |
|---|---|
onPlayWhenReadyChanged |
getPlaybackParameters
fun getPlaybackParameters(): PlaybackParameters!
Returns the currently active playback parameters.
| See also | |
|---|---|
onPlaybackParametersChanged |
getPlaybackState
@Player.State
fun getPlaybackState(): Int
Returns the current playback state of the player.
| Returns | |
|---|---|
Int |
The current |
| See also | |
|---|---|
onPlaybackStateChanged |
getPlaybackSuppressionReason
@Player.PlaybackSuppressionReason
fun getPlaybackSuppressionReason(): Int
Returns the reason why playback is suppressed even though getPlayWhenReady is
true, or PLAYBACK_SUPPRESSION_REASON_NONE if playback is not suppressed.
| Returns | |
|---|---|
Int |
The current |
| See also | |
|---|---|
onPlaybackSuppressionReasonChanged |
getPlayerError
fun getPlayerError(): PlaybackException?
Returns the error that caused playback to fail. This is the same error that will have been reported via onPlayerError at the time of failure. It can be queried using this method until the player is re-prepared.
Note that this method will always return null if getPlaybackState is not STATE_IDLE.
| Returns | |
|---|---|
PlaybackException? |
The error, or |
| See also | |
|---|---|
onPlayerError |
getPlaylistMetadata
fun getPlaylistMetadata(): MediaMetadata!
Returns the playlist MediaMetadata, as set by setPlaylistMetadata, or EMPTY if not supported.
This method must only be called if COMMAND_GET_METADATA is available.
| See also | |
|---|---|
onPlaylistMetadataChanged |
getPreviousMediaItemIndex
fun getPreviousMediaItemIndex(): 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. Returns INDEX_UNSET if hasPreviousMediaItem is false.
Note: When the repeat mode is REPEAT_MODE_ONE, this method behaves the same as when the current repeat mode is REPEAT_MODE_OFF. See REPEAT_MODE_ONE for more details.
This method must only be called if COMMAND_GET_TIMELINE is available.
getRepeatMode
@Player.RepeatMode
fun getRepeatMode(): Int
Returns the current RepeatMode used for playback.
| Returns | |
|---|---|
Int |
The current repeat mode. |
| See also | |
|---|---|
onRepeatModeChanged |
getSeekBackIncrement
fun getSeekBackIncrement(): Long
Returns the seekBack increment.
| Returns | |
|---|---|
Long |
The seek back increment, in milliseconds. |
| See also | |
|---|---|
onSeekBackIncrementChanged |
getSeekForwardIncrement
fun getSeekForwardIncrement(): Long
Returns the seekForward increment.
| Returns | |
|---|---|
Long |
The seek forward increment, in milliseconds. |
| See also | |
|---|---|
onSeekForwardIncrementChanged |
getShuffleModeEnabled
fun getShuffleModeEnabled(): Boolean
Returns whether shuffling of media items is enabled.
| See also | |
|---|---|
onShuffleModeEnabledChanged |
getSurfaceSize
@UnstableApi
fun getSurfaceSize(): Size!
Gets the size of the surface on which the video is rendered.
| See also | |
|---|---|
onSurfaceSizeChanged |
getTotalBufferedDuration
fun getTotalBufferedDuration(): Long
Returns an estimate of the total buffered duration from the current position, in milliseconds. This includes pre-buffered data for subsequent ads and media items.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getTrackSelectionParameters
fun getTrackSelectionParameters(): TrackSelectionParameters!
Returns the parameters constraining the track selection.
| See also | |
|---|---|
onTrackSelectionParametersChanged |
} |
getVideoSize
fun getVideoSize(): VideoSize!
Gets the size of the video.
The video's width and height are 0 if there is no supported video track or its size has not been determined yet.
| See also | |
|---|---|
onVideoSizeChanged |
getVolume
fun getVolume(): @FloatRange(from = 0, to = 1.0) Float
Returns the audio volume, with 0 being silence and 1 being unity gain (signal unchanged).
This method must only be called if COMMAND_GET_VOLUME is available.
| Returns | |
|---|---|
@FloatRange(from = 0, to = 1.0) Float |
The linear gain applied to all audio channels. |
| See also | |
|---|---|
onVolumeChanged |
hasNextMediaItem
fun hasNextMediaItem(): Boolean
Returns whether a next MediaItem exists, which may depend on the current repeat mode and whether shuffle mode is enabled.
Note: When the repeat mode is REPEAT_MODE_ONE, this method behaves the same as when the current repeat mode is REPEAT_MODE_OFF. See REPEAT_MODE_ONE for more details.
This method must only be called if COMMAND_GET_TIMELINE is available.
hasPreviousMediaItem
fun hasPreviousMediaItem(): Boolean
Returns whether a previous media item exists, which may depend on the current repeat mode and whether shuffle mode is enabled.
Note: When the repeat mode is REPEAT_MODE_ONE, this method behaves the same as when the current repeat mode is REPEAT_MODE_OFF. See REPEAT_MODE_ONE for more details.
This method must only be called if COMMAND_GET_TIMELINE is available.
increaseDeviceVolume
fun increaseDeviceVolume(@C.VolumeFlags flags: Int): Unit
Increases the volume of the device.
The getDeviceVolume device volume cannot be increased above maxVolume, if defined.
Note that this method affects the device volume. To change the volume of the current stream only, use setVolume.
This method must only be called if COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
@C.VolumeFlags flags: Int |
Either 0 or a bitwise combination of one or more |
isCommandAvailable
fun isCommandAvailable(@Player.Command command: Int): Boolean
Returns whether the provided Command is available.
This method does not execute the command.
| Parameters | |
|---|---|
@Player.Command command: Int |
A |
| See also | |
|---|---|
onAvailableCommandsChanged |
isCurrentMediaItemDynamic
fun isCurrentMediaItemDynamic(): Boolean
Returns whether the current MediaItem is dynamic (may change when the Timeline is updated), or false if the Timeline is empty.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
| See also | |
|---|---|
isDynamic |
isCurrentMediaItemLive
fun isCurrentMediaItemLive(): Boolean
Returns whether the current MediaItem is live, or false if the Timeline is empty.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
| See also | |
|---|---|
isLive |
isCurrentMediaItemSeekable
fun isCurrentMediaItemSeekable(): Boolean
Returns whether the current MediaItem is seekable, or false if the Timeline is empty.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
| See also | |
|---|---|
isSeekable |
isDeviceMuted
fun isDeviceMuted(): Boolean
Gets whether the device is muted or not.
Note that this method returns the mute state of the device. To check if the current stream is muted, use getVolume() == 0.
This method must only be called if COMMAND_GET_DEVICE_VOLUME is available.
| See also | |
|---|---|
onDeviceVolumeChanged |
isLoading
fun isLoading(): Boolean
Whether the player is currently loading the source.
| Returns | |
|---|---|
Boolean |
Whether the player is currently loading the source. |
| See also | |
|---|---|
onIsLoadingChanged |
isPlaying
fun isPlaying(): Boolean
Returns whether the player is playing, i.e. getCurrentPosition is advancing.
If false, then at least one of the following is true:
- The
playback stateis notready. - There is no
intention to play. - Playback is
suppressed for other reasons.
| Returns | |
|---|---|
Boolean |
Whether the player is playing. |
| See also | |
|---|---|
onIsPlayingChanged |
isPlayingAd
fun isPlayingAd(): Boolean
Returns whether the player is currently playing an ad.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
moveMediaItem
fun moveMediaItem(currentIndex: Int, newIndex: Int): Unit
Moves the media item at the current index to the new index.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
currentIndex: Int |
The current index of the media item to move. If the index is larger than the size of the playlist, the request is ignored. |
newIndex: Int |
The new index of the media item. If the new index is larger than the size of the playlist the item is moved to the end of the playlist. |
moveMediaItems
fun moveMediaItems(fromIndex: Int, toIndex: Int, newIndex: Int): Unit
Moves the media item range to the new index.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
fromIndex: Int |
The start of the range to move. If the index is larger than the size of the playlist, the request is ignored. |
toIndex: Int |
The first item not to be included in the range (exclusive). If the index is larger than the size of the playlist, items up to the end of the playlist are moved. |
newIndex: Int |
The new index of the first media item of the range. If the new index is larger than the size of the remaining playlist after removing the range, the range is moved to the end of the playlist. |
mute
@UnstableApi
fun mute(): Unit
Sets the audio volume to 0.
This method must only be called if COMMAND_SET_VOLUME is available.
pause
fun pause(): Unit
Pauses playback. Equivalent to setPlayWhenReady(false).
This method must only be called if COMMAND_PLAY_PAUSE is available.
play
fun play(): Unit
Resumes playback as soon as getPlaybackState == STATE_READY. Equivalent to setPlayWhenReady(true).
This method must only be called if COMMAND_PLAY_PAUSE is available.
prepare
fun prepare(): Unit
Prepares the player.
This method must only be called if COMMAND_PREPARE is available.
This will move the player out of idle state and the player will start loading media and acquire resources needed for playback.
release
fun release(): Unit
Releases the player. This method must be called when the player is no longer required. The player must not be used after calling this method.
This method must only be called if COMMAND_RELEASE is available.
removeListener
fun removeListener(listener: Player.Listener!): Unit
Unregister a listener registered through addListener. The listener will no longer receive events.
| Parameters | |
|---|---|
listener: Player.Listener! |
The listener to unregister. |
removeMediaItem
fun removeMediaItem(index: Int): Unit
Removes the media item at the given index of the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
index: Int |
The index at which to remove the media item. If the index is larger than the size of the playlist, the request is ignored. |
removeMediaItems
fun removeMediaItems(fromIndex: Int, toIndex: Int): Unit
Removes a range of media items from the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
fromIndex: Int |
The index at which to start removing media items. If the index is larger than the size of the playlist, the request is ignored. |
toIndex: Int |
The index of the first item to be kept (exclusive). If the index is larger than the size of the playlist, media items up to the end of the playlist are removed. |
replaceMediaItem
fun replaceMediaItem(index: Int, mediaItem: MediaItem!): Unit
Replaces the media item at the given index of the playlist.
Implementations of this method may attempt to seamlessly continue playback if the currently playing media item is replaced with a compatible one (e.g. same URL, only metadata has changed).
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
replaceMediaItems
fun replaceMediaItems(
fromIndex: Int,
toIndex: Int,
mediaItems: (Mutable)List<MediaItem!>!
): Unit
Replaces the media items at the given range of the playlist.
Implementations of this method may attempt to seamlessly continue playback if the currently playing media item is replaced with a compatible one (e.g. same URL, only metadata has changed).
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
Note that it is possible to replace a range with an arbitrary number of new items, so that the number of removed items defined by fromIndex and toIndex does not have to match the number of added items defined by mediaItems. As result, it may also change the index of subsequent items not touched by this operation.
| Parameters | |
|---|---|
fromIndex: Int |
The start of the range. If the index is larger than the size of the playlist, the request is ignored. |
toIndex: Int |
The first item not to be included in the range (exclusive). If the index is larger than the size of the playlist, items up to the end of the playlist are replaced. |
mediaItems: (Mutable)List<MediaItem!>! |
The |
seekBack
fun seekBack(): Unit
Seeks back in the current MediaItem by getSeekBackIncrement milliseconds.
This method must only be called if COMMAND_SEEK_BACK is available.
seekForward
fun seekForward(): Unit
Seeks forward in the current MediaItem by getSeekForwardIncrement milliseconds.
This method must only be called if COMMAND_SEEK_FORWARD is available.
seekTo
fun seekTo(positionMs: Long): Unit
Seeks to a position specified in milliseconds in the current MediaItem.
This method must only be called if COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM is available.
| Parameters | |
|---|---|
positionMs: Long |
The seek position in the current |
seekTo
fun seekTo(mediaItemIndex: Int, positionMs: Long): Unit
Seeks to a position specified in milliseconds in the specified MediaItem.
This method must only be called if COMMAND_SEEK_TO_MEDIA_ITEM is available.
| Parameters | |
|---|---|
mediaItemIndex: Int |
The index of the |
positionMs: Long |
The seek position in the specified |
seekToDefaultPosition
fun seekToDefaultPosition(): Unit
Seeks to the default position associated with the current MediaItem. The position can depend on the type of media being played. For live streams it will typically be the live edge. For other streams it will typically be the start.
This method must only be called if COMMAND_SEEK_TO_DEFAULT_POSITION is available.
seekToDefaultPosition
fun seekToDefaultPosition(mediaItemIndex: Int): Unit
Seeks to the default position associated with the specified MediaItem. The position can depend on the type of media being played. For live streams it will typically be the live edge. For other streams it will typically be the start.
This method must only be called if COMMAND_SEEK_TO_MEDIA_ITEM is available.
seekToNext
fun seekToNext(): Unit
Seeks to a later position in the current or next MediaItem (if available). More precisely:
- If the timeline is empty or seeking is not possible, does nothing.
- Otherwise, if
a next media item exists, seeks to the default position of the nextMediaItem. - Otherwise, if the current
MediaItemisliveand has not ended, seeks to the live edge of the currentMediaItem. - Otherwise, does nothing.
This method must only be called if COMMAND_SEEK_TO_NEXT is available.
seekToNextMediaItem
fun seekToNextMediaItem(): Unit
Seeks to the default position of the next MediaItem, which may depend on the current repeat mode and whether shuffle mode is enabled. Does nothing if hasNextMediaItem is false.
Note: When the repeat mode is REPEAT_MODE_ONE, this method behaves the same as when the current repeat mode is REPEAT_MODE_OFF. See REPEAT_MODE_ONE for more details.
This method must only be called if COMMAND_SEEK_TO_NEXT_MEDIA_ITEM is available.
seekToPrevious
fun seekToPrevious(): Unit
Seeks to an earlier position in the current or previous MediaItem (if available). More precisely:
- If the timeline is empty or seeking is not possible, does nothing.
- Otherwise, if the current
MediaItemisliveandunseekable, then:- If
a previous media item exists, seeks to the default position of the previous media item. - Otherwise, does nothing.
- If
- Otherwise, if
a previous media item existsand thecurrent positionis less thangetMaxSeekToPreviousPosition, seeks to the default position of the previousMediaItem. - Otherwise, seeks to 0 in the current
MediaItem.
This method must only be called if COMMAND_SEEK_TO_PREVIOUS is available.
seekToPreviousMediaItem
fun seekToPreviousMediaItem(): Unit
Seeks to the default position of the previous MediaItem, which may depend on the current repeat mode and whether shuffle mode is enabled. Does nothing if hasPreviousMediaItem is false.
Note: When the repeat mode is REPEAT_MODE_ONE, this method behaves the same as when the current repeat mode is REPEAT_MODE_OFF. See REPEAT_MODE_ONE for more details.
This method must only be called if COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM is available.
setAudioAttributes
fun setAudioAttributes(
audioAttributes: AudioAttributes!,
handleAudioFocus: Boolean
): Unit
Sets the attributes for audio playback, used by the underlying audio track. If not set, the default audio attributes will be used. They are suitable for general media playback.
Setting the audio attributes during playback may introduce a short gap in audio output as the audio track is recreated. A new audio session id will also be generated.
If tunneling is enabled by the track selector, the specified audio attributes will be ignored, but they will take effect if audio is later played without tunneling.
If audio focus should be handled, the usage must be USAGE_MEDIA or USAGE_GAME. Other usages will throw an IllegalArgumentException.
This method must only be called if COMMAND_SET_AUDIO_ATTRIBUTES is available.
| Parameters | |
|---|---|
audioAttributes: AudioAttributes! |
The attributes to use for audio playback. |
handleAudioFocus: Boolean |
True if the player should handle audio focus, false otherwise. |
setDeviceMuted
fun setDeviceMuted(muted: Boolean, @C.VolumeFlags flags: Int): Unit
Sets the mute state of the device.
Note that this method affects the device volume. To mute just the current stream, use
setVolume(0) instead.
This method must only be called if COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
muted: Boolean |
Whether to set the device to be muted or not |
@C.VolumeFlags flags: Int |
Either 0 or a bitwise combination of one or more |
setDeviceVolume
fun setDeviceVolume(volume: @IntRange(from = 0) Int, @C.VolumeFlags flags: Int): Unit
Sets the volume of the device with volume flags.
Note that this method affects the device volume. To change the volume of the current stream only, use setVolume.
This method must only be called if COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
volume: @IntRange(from = 0) Int |
The volume to set. |
@C.VolumeFlags flags: Int |
Either 0 or a bitwise combination of one or more |
setMediaItem
fun setMediaItem(mediaItem: MediaItem!): Unit
Clears the playlist, adds the specified MediaItem and resets the position to the default position.
To replace a media item (possibly seamlessly) without clearing the playlist, use replaceMediaItem.
This method must only be called if COMMAND_SET_MEDIA_ITEM is available.
setMediaItem
fun setMediaItem(mediaItem: MediaItem!, resetPosition: Boolean): Unit
Clears the playlist and adds the specified MediaItem.
To replace a media item (possibly seamlessly) without clearing the playlist, use replaceMediaItem.
This method must only be called if COMMAND_SET_MEDIA_ITEM is available.
| Parameters | |
|---|---|
mediaItem: MediaItem! |
The new |
resetPosition: Boolean |
Whether the playback position should be reset to the default position. If false, playback will start from the position defined by |
setMediaItem
fun setMediaItem(mediaItem: MediaItem!, startPositionMs: Long): Unit
Clears the playlist and adds the specified MediaItem.
To replace a media item (possibly seamlessly) without clearing the playlist, use replaceMediaItem.
This method must only be called if COMMAND_SET_MEDIA_ITEM is available.
| Parameters | |
|---|---|
mediaItem: MediaItem! |
The new |
startPositionMs: Long |
The position in milliseconds to start playback from. If |
setMediaItems
fun setMediaItems(mediaItems: (Mutable)List<MediaItem!>!): Unit
Clears the playlist, adds the specified media items and resets the position to the default position.
To replace a span of media items (possibly seamlessly) without clearing the playlist, use replaceMediaItems.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
mediaItems: (Mutable)List<MediaItem!>! |
The new |
setMediaItems
fun setMediaItems(
mediaItems: (Mutable)List<MediaItem!>!,
resetPosition: Boolean
): Unit
Clears the playlist and adds the specified media items.
To replace a span of media items (possibly seamlessly) without clearing the playlist, use replaceMediaItems.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
mediaItems: (Mutable)List<MediaItem!>! |
The new |
resetPosition: Boolean |
Whether the playback position should be reset to the default position in the first |
setMediaItems
fun setMediaItems(
mediaItems: (Mutable)List<MediaItem!>!,
startIndex: Int,
startPositionMs: Long
): Unit
Clears the playlist and adds the specified media items.
To replace a span of media items (possibly seamlessly) without clearing the playlist, use replaceMediaItems.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
mediaItems: (Mutable)List<MediaItem!>! |
The new |
startIndex: Int |
The |
startPositionMs: Long |
The position in milliseconds to start playback from. If |
| Throws | |
|---|---|
androidx.media3.common.IllegalSeekPositionException |
If the provided |
setPlayWhenReady
fun setPlayWhenReady(playWhenReady: Boolean): Unit
Sets whether playback should proceed when getPlaybackState == STATE_READY.
If the player is already in the ready state then this method pauses and resumes playback.
This method must only be called if COMMAND_PLAY_PAUSE is available.
| Parameters | |
|---|---|
playWhenReady: Boolean |
Whether playback should proceed when ready. |
setPlaybackParameters
fun setPlaybackParameters(playbackParameters: PlaybackParameters!): Unit
Attempts to set the playback parameters. Passing DEFAULT resets the player to the default, which means there is no speed or pitch adjustment.
Playback parameters changes may cause the player to buffer. onPlaybackParametersChanged will be called whenever the currently active playback parameters change.
This method must only be called if COMMAND_SET_SPEED_AND_PITCH is available.
| Parameters | |
|---|---|
playbackParameters: PlaybackParameters! |
The playback parameters. |
setPlaybackSpeed
fun setPlaybackSpeed(speed: @FloatRange(from = 0, fromInclusive = false) Float): Unit
Changes the rate at which playback occurs. The pitch is not changed.
This is equivalent to
setPlaybackParameters(getPlaybackParameters().withSpeed(speed)).
This method must only be called if COMMAND_SET_SPEED_AND_PITCH is available.
| Parameters | |
|---|---|
speed: @FloatRange(from = 0, fromInclusive = false) Float |
The linear factor by which playback will be sped up. Must be higher than 0. 1 is normal speed, 2 is twice as fast, 0.5 is half normal speed. |
setPlaylistMetadata
fun setPlaylistMetadata(mediaMetadata: MediaMetadata!): Unit
Sets the playlist MediaMetadata.
This method must only be called if COMMAND_SET_PLAYLIST_METADATA is available.
setRepeatMode
fun setRepeatMode(@Player.RepeatMode repeatMode: Int): Unit
Sets the RepeatMode to be used for playback.
This method must only be called if COMMAND_SET_REPEAT_MODE is available.
| Parameters | |
|---|---|
@Player.RepeatMode repeatMode: Int |
The repeat mode. |
setShuffleModeEnabled
fun setShuffleModeEnabled(shuffleModeEnabled: Boolean): Unit
Sets whether shuffling of media items is enabled.
This method must only be called if COMMAND_SET_SHUFFLE_MODE is available.
| Parameters | |
|---|---|
shuffleModeEnabled: Boolean |
Whether shuffling is enabled. |
setTrackSelectionParameters
fun setTrackSelectionParameters(parameters: TrackSelectionParameters!): Unit
Sets the parameters constraining the track selection.
Unsupported parameters will be silently ignored.
Use getTrackSelectionParameters to retrieve the current parameters. For example, the following snippet restricts video to SD whilst keep other track selection parameters unchanged:
player.setTrackSelectionParameters( player.getTrackSelectionParameters() .buildUpon() .setMaxVideoSizeSd() .build())
This method must only be called if COMMAND_SET_TRACK_SELECTION_PARAMETERS is available.
setVideoSurface
fun setVideoSurface(surface: Surface?): Unit
Sets the Surface onto which video will be rendered. The caller is responsible for tracking the lifecycle of the surface, and must clear the surface by calling
setVideoSurface(null) if the surface is destroyed.
If the surface is held by a SurfaceView, TextureView or SurfaceHolder then it's recommended to use setVideoSurfaceView, setVideoTextureView or setVideoSurfaceHolder rather than this method, since passing the holder allows the player to track the lifecycle of the surface automatically.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
setVideoSurfaceHolder
fun setVideoSurfaceHolder(surfaceHolder: SurfaceHolder?): Unit
Sets the SurfaceHolder that holds the Surface onto which video will be rendered. The player will track the lifecycle of the surface automatically.
The thread that calls the SurfaceHolder.Callback methods must be the thread associated with getApplicationLooper.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
surfaceHolder: SurfaceHolder? |
The surface holder. |
setVideoSurfaceView
fun setVideoSurfaceView(surfaceView: SurfaceView?): Unit
Sets the SurfaceView onto which video will be rendered. The player will track the lifecycle of the surface automatically.
The thread that calls the SurfaceHolder.Callback methods must be the thread associated with getApplicationLooper.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
surfaceView: SurfaceView? |
The surface view. |
setVideoTextureView
fun setVideoTextureView(textureView: TextureView?): Unit
Sets the TextureView onto which video will be rendered. The player will track the lifecycle of the surface automatically.
Consider using SurfaceView via setVideoSurfaceView instead of TextureView. SurfaceView generally causes lower battery consumption, and has better handling for HDR and secure content. See Choosing a surface type for more information.
The thread that calls the TextureView.SurfaceTextureListener methods must be the thread associated with getApplicationLooper.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
textureView: TextureView? |
The texture view. |
setVolume
fun setVolume(volume: @FloatRange(from = 0, to = 1.0) Float): Unit
Sets the audio volume, valid values are between 0 (silence) and 1 (unity gain, signal unchanged), inclusive.
This method must only be called if COMMAND_SET_VOLUME is available.
| Parameters | |
|---|---|
volume: @FloatRange(from = 0, to = 1.0) Float |
Linear output gain to apply to all audio channels. |
stop
fun stop(): Unit
Stops playback without resetting the playlist. Use pause rather than this method if the intention is to pause playback.
Calling this method will cause the playback state to transition to STATE_IDLE and the player will release the loaded media and resources required for playback. The player instance can still be used by calling prepare again, and release must still be called on the player if it's no longer required.
Calling this method does not clear the playlist, reset the playback position or the playback error.
This method must only be called if COMMAND_STOP is available.
unmute
@UnstableApi
fun unmute(): Unit
If the audio volume is 0, sets the audio volume to a non-zero value decided by the Player to be the most appropriate.
This method must only be called if COMMAND_SET_VOLUME is available.
Extension functions
listen
@UnstableApi
suspend fun Player.listen(onEvents: Player.(Player.Events) -> Unit): Nothing
Continuously listens to the Player.Listener.onEvents callback, passing the received Player.Events to the provided onEvents function.
This function can be called from any thread. The onEvents function will be invoked on the thread associated with Player.getApplicationLooper.
If, during the execution of onEvents, an exception is thrown, the coroutine corresponding to listening to the Player will be terminated. Any used resources will be cleaned up (e.g. removing of the listeners) and exception will be re-thrown right after the last suspension point.
| Parameters | |
|---|---|
onEvents: Player.(Player.Events) -> Unit |
The function to handle player events. |
| Returns | |
|---|---|
Nothing |
Nothing This function never returns normally. It will either continue indefinitely or terminate due to an exception or cancellation. |
listenTo
@UnstableApi
suspend fun Player.listenTo(
firstEvent: @Player.Event Int,
vararg otherEvents: @Player.Event Int,
onEvents: Player.(Player.Events) -> Unit
): Nothing
Continuously listens to the Player.Listener.onEvents callback, passing the received Player.Events to the provided onEvents function. A non-zero number of events has to be specified (hence firstEvent and otherEvents). The order is not important.
This function can be called from any thread. The onEvents function will be invoked on the thread associated with Player.getApplicationLooper.
If, during the execution of onEvents, an exception is thrown, the coroutine corresponding to listening to the Player will be terminated. Any used resources will be cleaned up (e.g. removing of the listeners) and exception will be re-thrown right after the last suspension point.
| Parameters | |
|---|---|
firstEvent: @Player.Event Int |
One of the |
vararg otherEvents: @Player.Event Int |
The set of other |
onEvents: Player.(Player.Events) -> Unit |
The function to handle player events. |
| Returns | |
|---|---|
Nothing |
Nothing This function never returns normally. It will either continue indefinitely or terminate due to an exception or cancellation. |
observeState
@UnstableApi
fun Player.observeState(
firstEvent: @Player.Event Int,
vararg otherEvents: @Player.Event Int,
stateUpdater: (Player) -> Unit
): PlayerStateObserver
Utility to observe Player states by listening to events.
| Parameters | |
|---|---|
firstEvent: @Player.Event Int |
The first |
vararg otherEvents: @Player.Event Int |
Additional |
stateUpdater: (Player) -> Unit |
The operation to trigger initially and whenever one of the configured events happen |