Listening to EXOplayer events

Kotlin 2020 solution approach UPDATE

Events such as changes in state and playback errors are reported to registered Player.EventListener instances.

Player.EventListener has empty default methods, so you only need to implement the methods you’re interested in.

First your class, say your activity, has te conform to the Player.EventListener interface.

class VideoPlayerActivity : AppCompatActivity(), Player.EventListener {

Then you override the onIsPlayingChanged method, on the class. Outside onCreate method…

Add the listener to your player instance:

// Adding player listener for tracking events
player?.addListener(this)

You can check if the player is playing (i.e. the position is advancing) with Player.isPlaying:

//Listening to player events
override fun onIsPlayingChanged(isPlaying: Boolean){
    if (isPlaying) {
        // Active playback.
    } else {
        // Not playing because playback is paused, ended, suppressed, or the player
        // is buffering, stopped or failed. Check player.getPlaybackState,
        // player.getPlayWhenReady, player.getPlaybackError and
        // player.getPlaybackSuppressionReason for details.
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *