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?
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)
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);
}
}
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With