Detect pause/resume in ExoPlayer

后端 未结 7 1936
予麋鹿
予麋鹿 2021-02-06 23:02

I searched two days for this question in github but i can\'t find true answer . I want example for detecting pause / resume in ExoPlayer > 2.x . An

相关标签:
7条回答
  • 2021-02-07 00:01

    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.

    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.
        }
    }
    

    That's it. Very simple.

    0 讨论(0)
提交回复
热议问题