Search code examples
javaandroidexoplayer

How to get current duration of video in exo player?


I'm using the ExoPlayer library for playing some videos from the online data sources. Everything is working fine.

Now I want to get current video duration/position on every second. I can't get any option in the default event listener. Anyone have any reference, I've searched about it but can't get anything. Please help me to get this.

Here's the event listener functions I'm getting,

 @Override
    public void onRepeatModeChanged(int repeatMode) {

    }

    @Override
    public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

    }


    @Override
    public void onPlayerError(ExoPlaybackException error) {

    }

    @Override
    public void onPositionDiscontinuity(int reason) {

    }

    
    @Override
    public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

    }

    @Override
    public void onSeekProcessed() {

    }


    @Override
    public void onPlaybackSuppressionReasonChanged(int playbackSuppressionReason) {

    }

    @Override
    public void onIsPlayingChanged(boolean isPlaying) {

    }

Solution

  • If you want to get current video position at every second , then run handler for every second and get the current position in seconds as shown below.

    KOTLIN

     var videoWatchedTime = 0L
    
    Handler().postDelayed({
        // Do your work
        videoWatchedTime = player.currentPosition / 1000
    }, 1000)
    

    JAVA

    long videoWatchedTime =0;
    new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do your work
       videoWatchedTime= player.getCurrentPosition()/1000;
            }
        },1000);