mediaDuration method

String mediaDuration({
  1. bool addSpacing = true,
})

Formats the media duration into a human-readable string.

  • Converts total seconds into hours, minutes, and seconds.
  • Displays hours only if the duration is 1 hour or more.
  • Supports optional spacing between time units.

Example outputs:

  • 45 seconds → "00:45"
  • 125 seconds → "02:05"
  • 4000 seconds → "01:06:40"

addSpacing If true, adds spaces around colons ("01 : 06 : 40"). If false, returns a compact format ("01:06:40").

Returns a formatted string representing the media duration.

Implementation

String mediaDuration({bool addSpacing = true}) {
  String hours = (this ~/ 3600).toTimeUnit();
  String minutes = ((this % 3600) ~/ 60).toTimeUnit();
  String seconds = (this % 60).toTimeUnit();

  String separator = addSpacing ? " : " : ":";

  return hours != "00" ? "$hours$separator$minutes$separator$seconds" : "$minutes$separator$seconds";
}