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 method | Description |
---|---|
onStateChange | Called on every PlaybackState , BufferingState (Buffering start, Buffering end) and SeekingState (Seeking start, Seeking end) change. |
onProgressUpdate | Called when the Player's playback position has changed. |
onPlayerViewAvailable | Called when player's rendering surface becomes available. |
onActiveTrackVariantChanged | Called when the selected media track has changed. |
onTrackAvailabilityChanged | Called when the available media tracks have changed. |
onVideoSizeChanged | Called when the video size has changed. |
onStreamEnded | Called when the player has finished playing the media. (VOD Only) |
onError | Called when playback error occurred. |
onEventReceived | Called 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.
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
)
)
}