ExoPlayer and start / pause / seekTo commands

前端 未结 2 1640
情话喂你
情话喂你 2020-12-25 11:58

I am trying to use ExoPlayer, as opposed to MediaPlayer and I can\'t seem to figure it out...

MediaPlayer has .start()

2条回答
  •  我在风中等你
    2020-12-25 12:47

    The official example of the PlayerControl in the ExoPlayer source code do exactly what you asked:

    public class PlayerControl implements MediaPlayerControl {
    
      private final ExoPlayer exoPlayer;
    
      public PlayerControl(ExoPlayer exoPlayer) {
        this.exoPlayer = exoPlayer;
      }
    
      @Override
      public boolean canPause() {
        return true;
      }
    
      @Override
      public boolean canSeekBackward() {
        return true;
      }
    
      @Override
      public boolean canSeekForward() {
        return true;
      }
    
      @Override
      public int getAudioSessionId() {
        throw new UnsupportedOperationException();
      }
    
      @Override
      public int getBufferPercentage() {
        return exoPlayer.getBufferedPercentage();
      }
    
      @Override
      public int getCurrentPosition() {
        return exoPlayer.getDuration() == ExoPlayer.UNKNOWN_TIME ? 0
            : (int) exoPlayer.getCurrentPosition();
      }
    
      @Override
      public int getDuration() {
        return exoPlayer.getDuration() == ExoPlayer.UNKNOWN_TIME ? 0
            : (int) exoPlayer.getDuration();
      }
    
      @Override
      public boolean isPlaying() {
        return exoPlayer.getPlayWhenReady();
      }
    
      @Override
      public void start() {
        exoPlayer.setPlayWhenReady(true);
      }
    
      @Override
      public void pause() {
        exoPlayer.setPlayWhenReady(false);
      }
    
      @Override
      public void seekTo(int timeMillis) {
        long seekPosition = exoPlayer.getDuration() == ExoPlayer.UNKNOWN_TIME ? 0
            : Math.min(Math.max(0, timeMillis), getDuration());
        exoPlayer.seekTo(seekPosition);
      }
    
    }
    

    If you are experiencing strange behaviors during the seek operation, it may be due to you particular stream/file type. I can suggest you to take a look at the base implementation of the ExoPlayer and, eventually, report any issue on Github.

提交回复
热议问题