Display the seconds of TMediaPlayer.Duration and TMediaPlayer.CurrentTime using Delphi for Android

556 views Asked by At

I'm using Delphi 10.3 for developing android mobile application. I'm using the TMediaPlayer to play the mp3 files. I want to display the current time and remaining time of the currently playing media file with the minutes and seconds (mm:ss - ref: VLC media player). But I can able display the minutes properly and I want to display the seconds with the two digits.

Please help me to display the seconds properly.

Here, I have mentioned the code that I have tried.

procedure Timer1Timer(Sender: TObject);
begin
  TrackBar1.Tag := 1;
  TrackBar1.Value := MediaPlayer1.CurrentTime;
  CurrentMin := MediaPlayer1.CurrentTime div 1000 div 600000;
  CurrentSec := MediaPlayer1.CurrentTime div 1000; // Seconds
  DurationMin := MediaPlayer1.Duration div 1000 div 600000;
  DurationSec := MediaPlayer1.Duration div 1000; // Seconds
  LabelCurrentTime.Text   := Format('%2.2d : %2.2d', [CurrentMin, CurrentSec]);
  LabelRemainingTime.Text := Format('%2.2d : %2.2d', [DurationMin, DurationSec]);
  TrackBar1.Tag := 0;  
end;
1

There are 1 answers

1
Tom Brunberg On

The documentation for FMX.Media.TMediaPlayer.CurrentTime says:

CurrentTime is measured in 100ns. To obtain s, divide CurrentTime by MediaTimeScale. (s refers to seconds)

and

MediaTimeScale: Integer = $989680;

So, given variables

var
  CurrentTime, RemainingTime: int64;
  CurMins, CurSecs, RemMins, RemSecs: integer;

we can write the following

  CurrentTime := MediaPlayer1.CurrentTime;
  RemainingTime := MediaPlayer1.Duration - CurrentTime;

  CurMins := CurrentTime div MediaTimeScale div 60;
  CurSecs := CurrentTime div MediaTimeScale mod 60;
  LabelCurrentTime.Text := Format('Current: %d:%.2d',[CurMins, CurSecs]);

  RemMins := RemainingTime div MediaTimeScale div 60;
  RemSecs := RemainingTime div MediaTimeScale mod 60;
  LabelRemainingTime.Text := Format('Remaining: %d:%.2d',[RemMins, RemSecs]);

Note the usage of 'mod' to limit seconds display to '00 .. 59'