Skip to main content

Listening to Player Events

Attach Listeners

The 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

Changes in player state can be received 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, 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 etc.,) 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

Changes in player progress can be received by implementing the following in a registered listener/delegate:

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

Handle Player Errors

Playback failures can be observed by implementing the following in a registered listener/delegate.

override fun onError(error: Error) {
}
note

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

Observe Player View Availability

The view can be attached 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
)
)
}