xabush
xabush

Reputation: 877

Display the remaining duration of a video in Exoplayer

I am using SimpleExoPlayer to play videos from raw folder with the default controls. Everything works fine, however, I wanted to display the remaining duration in the right corner of the control bar instead of the total duration. I can get the total duration with getDuration method of the player and the remaining duration with player.getDuration() - player.getCurrentPosition(). But I don't how to update the remaining duration each second and display on the view.

How can I achieve this?

Upvotes: 0

Views: 5750

Answers (2)

Ayushi Khandelwal
Ayushi Khandelwal

Reputation: 336

  1. Calculates times left

  2. Removes and adds the callback

        private Handler mHandler;
    
            private final Runnable updateProgressAction = () ->     updateProgress();
    
            private void updateProgress() {
                if (tv_videoDuration != null) {
                    **long timeLeft = videoPlayer.getDuration() - videoPlayer.getContentPosition();**
                    if (timeLeft > 0) {                    tv_videoDuration.setText(calculateTimeLeft(timeLeft));
                    }
                }
                long delayMs = TimeUnit.SECONDS.toMillis(1);
                mHandler.postDelayed(updateProgressAction, delayMs);
            }
    
    
      fun calculateTimeLeft(timeLeft: Long): String {
        return String.format(
          "%d:%02d",
          TimeUnit.MILLISECONDS.toMinutes(timeLeft) % TimeUnit.HOURS.toMinutes(1),
          TimeUnit.MILLISECONDS.toSeconds(timeLeft) % TimeUnit.MINUTES.toSeconds(1)
        )
      }
    
    
      public void pausePlayer() {
            if (videoPlayer != null) {
                videoPlayer.setPlayWhenReady(false);
                mHandler.removeCallbacks(updateProgressAction);
            }
        }
    
        public void resumePlayer() {
            if (videoPlayer != null) {
                videoPlayer.setPlayWhenReady(true);
                mHandler = new Handler();
                mHandler.post(updateProgressAction);
            }
        }
    
        public void restartPlayer() {
            if (videoPlayer != null) {
                videoPlayer.seekTo(0);
                videoPlayer.setPlayWhenReady(true);
                mHandler = new Handler();
                mHandler.post(updateProgressAction);
            }
        }

Upvotes: 1

Alon Aviram
Alon Aviram

Reputation: 200

This is a simple example how to implement update progress every second.
You should still use Handler.removeCallbacks(updateProgressAction) while player is not playing (you can get events from ExoPlayer.addListener(Player.EventListener);) and when your Activity is paused / destroyed.

   public class MainActivity extends AppCompatActivity {
        private final Runnable updateProgressAction = new Runnable() {
            @Override
            public void run() {
                updateProgress();
            }
        };
        private TextView mTextView;
        private Handler mHandler;
        private SimpleExoPlayer mSimpleExoPlayer;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTextView = findViewById(R.id.my_text);
            SimpleExoPlayerView simpleExoPlayerView = findViewById(R.id.player_view);
            mSimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(new DefaultBandwidthMeter()));
            simpleExoPlayerView.setPlayer(mSimpleExoPlayer);
            Uri uri = Uri.parse("https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8");
            String userAgent = Util.getUserAgent(this, "appName");
            HlsMediaSource mediaSource = new HlsMediaSource(uri, new DefaultDataSourceFactory(this, userAgent), null, null);
            mSimpleExoPlayer.prepare(mediaSource);
            mSimpleExoPlayer.setPlayWhenReady(true);
            mHandler = new Handler();
            mHandler.post(updateProgressAction);
        }

        private void updateProgress() {
            mTextView.setText("calculate time and format");
            long delayMs = TimeUnit.SECONDS.toMillis(1);
            mHandler.postDelayed(updateProgressAction, delayMs);
        }
    }

Upvotes: 1

Related Questions