Skip to main content

Listening to player events

Attach listeners

Your application can listen to events such as changes in player state, buffering state, seek state, and playback errors by registering a listener/delegate.

// Add a listener to receive events from the player.
val listener = object: Player.Listener {
override fun onStateChange(
playbackState: PlaybackState,
bufferingState: BufferingState,
seekingState: SeekingState
) {
//Handle as required
}

override fun onProgressUpdate(currentTimeInWindowMs: Long, currentWindowDurationMs: Long) {
//Handle as required
}

override fun onPlayerViewAvailable(playerView: FrameLayout) {
//Handle as required
}

override fun onActiveTrackVariantChanged(variantInfo: TrackVariantInfo) {
//Handle as required
}

override fun onStreamEnded() {
//Handle as required
}

override fun onError(error: Error) {
//Handle as required
}

override fun onEventReceived(
streamTimelineEvent: StreamTimelineEvent,
suggestedAction: Action,
streamTimelineMetadata: StreamTimelineMetadata?
) {
//Handle as required
}
}

player.addListener(listener)
Listener methodDescription
onStateChangeCalled on every PlaybackState, BufferingState (Buffering start, Buffering end) and SeekingState (Seeking start, Seeking end) change.
onProgressUpdateCalled when the Player's playback position has changed.
onPlayerViewAvailableCalled when player's rendering surface becomes available.
onActiveTrackVariantChangedCalled when the selected media track has changed.
onTrackAvailabilityChangedCalled when the available media tracks have changed.
onVideoSizeChangedCalled when the video size has changed.
onStreamEndedCalled when the player has finished playing the media. (VOD Only)
onErrorCalled when playback error occurred.
onEventReceivedCalled when a StreamTimelineEvent is received.

Observe player state changes

You can receive changes in player state by implementing the following in a registered listener/delegate:

override fun onStateChange(playbackState: PlaybackState,
bufferingState: BufferingState,
seekingState: SeekingState) {
}

The player can be in one of the following playback states:

  • PlaybackState.IDLE: This is the initial state and indicates that the player has no media to play and doesn't hold any resources.
  • PlaybackState.LOADING: Indicates that the player is loading the initial media chunks (manifest, initialization chunks, and more) required to play the media source.
  • PlaybackState.LOADED: Indicates that the player has loaded all the resources needed to start playback rendering.
  • PlaybackState.STARTED: Indicates that the player has started rendering playback.
  • PlaybackState.PAUSED: Indicates that the player has paused rendering playback.
  • PlaybackState.STOPPING: Indicates that the player is in the process of stopping playback rendering.

Player State Machine

Observing the playback time

You can receive changes in player progress by implementing the following in a registered listener/delegate:

override fun onProgressUpdate(
currentTimeInWindowMillis: Long,
currentWindowDurationMillis: Long) {
}

Handle player errors

You can observe playback failures by implementing the following in a registered listener/delegate.

override fun onError(error: Error) {
}
note

Refer to the Error Codes page for the full list of possible error codes.

Observe player view availability

You can attach the view to the player using attachRenderingView method after receiving the callback from the attached listener.

  override fun onPlayerViewAvailable(playerView: FrameLayout) {
attachToPlayer()
}

private fun attachToPlayer() {
player.attachRendererView(
playerRoot,
FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
)
}