Detect pause/resume in ExoPlayer

后端 未结 7 1938
予麋鹿
予麋鹿 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-06 23:59

    You need to check playWhenReady with a Player.EventListener. The Playback states of ExoPlayer are independent from the player being paused or not:

    player.addListener(new Player.DefaultEventListener() {
      @Override
      public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        if (playWhenReady && playbackState == Player.STATE_READY) {
          // media actually playing
        } else if (playWhenReady) {
          // might be idle (plays after prepare()), 
          // buffering (plays when data available) 
          // or ended (plays when seek away from end)
        } else {
          // player paused in any state
        }
      }
    });
    

    To play/pause the player ExoPlayer provides

    player.setPlayWhenReady(boolean)
    

    The sequence of playback states with ExoPlayer with a media file which never stalls to rebuffer is once in each of the four states and does not express play/paused:

    Player.STATE_IDLE;
    Player.STATE_BUFFERING;
    Player.STATE_READY;
    Player.STATE_ENDED;
    

    Each time the player needs to buffer it goes:

    Player.STATE_READY;
    Player.STATE_BUFFERING;
    Player.STATE_READY;
    

    Setting playWhenReady does not affect the state.

    All together your media is actually playing when

    playWhenReady && playbackState == Player.STATE_READY
    

    It plays when ready. :)

提交回复
热议问题