Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the unit of measurment for media duration in MediaMetadataRetriever

One can use

 String MediaMetadataRetriver.extractMetadata(int key);

with

 key = MediaMetadataRetriver.METADATA_KEY_DURATION

to extract the media duration.

This function returns a String, but there is no documentation regarding the format or unit of measurement this string represents. I would assume it is an integer in ms, but the rest of MediaMetadataRetriever's apis uses us.

So what is the unit for duration? Do you think the lack of specification is intentional or a bug?

like image 251
misiu_mp Avatar asked May 11 '12 14:05

misiu_mp


3 Answers

Because extractMetadata can return a null you need to wrap this call in a check:

int duration = 0;
String dur = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
if (dur != null) {
    duration = Integer.parseInt(dur);
}
mDuration = duration;
long h = duration / 3600;
long m = (duration - h * 3600) / 60;
long s = duration - (h * 3600 + m * 60);

(from http://www.codota.com/android/scenarios/52fcbd47da0af79604fb4a67/android.media.MediaMetadataRetriever?tag=dragonfly)

like image 125
vitriolix Avatar answered Oct 23 '22 10:10

vitriolix


See this

  long durationMs = Long.parseLong(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
                    long duration = durationMs / 1000;
                    long h = duration / 3600;
                    long m = (duration - h * 3600) / 60;
                    long s = duration - (h * 3600 + m * 60);
                    String durationValue;
                    if (h == 0) {
                       durationValue = String.format(
                       activity.getString(R.string.details_ms), m, s);
                       } else {
                            durationValue = String.format(
                            activity.getString(R.string.details_hms), h, m, s);
                         }
                    }  
like image 14
Silverstorm Avatar answered Oct 23 '22 10:10

Silverstorm


The output of the extraction is String, you need to parse into long. And you can handle like that to have formated duration:

public static String formatDurationLongToString() {
        String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long timeInMillisecond = Long.parseLong(time)
        return String.format(Locale.US, "%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(timeInMillisecond),
                TimeUnit.MILLISECONDS.toMinutes(timeInMillisecond) % TimeUnit.HOURS.toMinutes(1),
                TimeUnit.MILLISECONDS.toSeconds(timeInMillisecond) % TimeUnit.MINUTES.toSeconds(1));
    }
like image 1
Huy Nguyen Avatar answered Oct 23 '22 08:10

Huy Nguyen