1

I want to get current duration of video. I am using ExoPlayer(Exoplayer is used to play videos it is a video player) and i want to display current video playing duration. The method getCurrentDuration() is used to get current duration of video so I want to get that duration in log. That is why i code like this but it is not providing correct duration. I want that when video is playing and the current duration which is showing on seek bar should be displayed in log also.

for (long x=0;x<10000000;x++){

long position = mPlayer.getCurrentPosition();

Log.d(">>>>AdPopUp", Long.toString(position));
  • Welcome to Stack Overflow! It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please [edit] your question to give a more complete description of what you expected to happen and how that differs from the actual results. See [ask] for hints on what makes a good explanation. – Toby Speight Jun 20 '17 at 11:01
  • Always use `String.valueOf( i );` to convert any int,long,double value into String. – Nitin Patel Jun 20 '17 at 11:38
  • no one has answer of this? – vaibhav thakre Jun 24 '17 at 05:59
  • Here is answer which explains how, using ExoPlayer and suspendCoroutine to return value. https://stackoverflow.com/a/71708709/6039240 – Amr Apr 01 '22 at 14:42

1 Answers1

0

There is a way:

private PlayerView videoView;

Log.i("video_duration", 
((TextView)videoView.findViewById(R.id.exo_duration)).getText().toString());

Remember not to call it immediately. It will return 00:00 instead use it inside a handler with postDelayed(Runnable, long millisec) like below:

videoView.postDelayed(new Runnable() {
@Override
public void run() {
Log.i("video_duration", 
((TextView)videoView.findViewById(R.id.exo_duration)).getText().toString());
}
}, 2000);

However, you may use MediaMetadataRetriever:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
     retriever.setDataSource(mContext, Uri.fromFile(file));
     String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
     } catch (Exception ignored) {
    } finally {
        retriever.release();
    }
Bukhari
  • 1
  • 1