Detect pause/resume in ExoPlayer

后端 未结 7 1935
予麋鹿
予麋鹿 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:37

    You can use exoplayer.getPlayWhenReady() to check whether the player is currently in a pause state or playing state.

    0 讨论(0)
  • 2021-02-06 23:37

    Please try below code snipped. The player listener(onPlayerStateChanged) isn't good to observe play / pause action its called multiple time and also invoke while player configuring.

    videoView.setControlDispatcher(object : DefaultControlDispatcher() {
                override fun dispatchSetPlayWhenReady(player: Player, playWhenReady: Boolean): Boolean {
                    if (playWhenReady)
                      // Tap on Play button  
                    else
                     // Tap on Pause button
                    return super.dispatchSetPlayWhenReady(player, playWhenReady)
                }
            })
    
    0 讨论(0)
  • 2021-02-06 23:50

    You can use this function:

    public boolean isPlaying() {
        return exoPlayer.getPlaybackState() == Player.STATE_READY && exoPlayer.getPlayWhenReady();
    }
    
    0 讨论(0)
  • 2021-02-06 23:56

    I had the same requirement to detect the click event of exoplayer play/pause button. Above answers were mainly talking about the state not about the button click event.

    This is what I did to detect the Play/Pause button click, works perfect.

    Step 1: Create custom control dispatcher class and override the method dispatchSetPlayWhenReady

    class PlayerControlDispatcher : DefaultControlDispatcher() {
        override fun dispatchSetPlayWhenReady(player: Player?, playWhenReady: Boolean): Boolean {
            if(playWhenReady) {
               // Play button clicked
            } else {
              // Paused button clicked
            }
            return super.dispatchSetPlayWhenReady(player, playWhenReady)
        }
    }
    

    Step 2: Set the custom control dispatcher class PlayerControlDispatcher into the the player view.

    playerView.setControlDispatcher(PlayerControlDispatcher())
    

    Where playerView is an instance of com.google.android.exoplayer2.ui.PlayerView which we declare in our layout file.

    0 讨论(0)
  • 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. :)

    0 讨论(0)
  • 2021-02-06 23:59

    It must be that since the other answers were posted, a new method has been provided in Player.EventListener. This works well for me:

    override fun onIsPlayingChanged(isPlaying: Boolean) {
        // your code here
    }
    

    If isPlaying is false, it is paused, otherwise playing.

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