2

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;
test12345
  • 367
  • 1
  • 21
  • 1
    You neglected to mention what the output is that you are seeing, and what the variables are declared as. Regardless, assuming that they're integers, you could use: `Format('%d:%.2d', [CurrentMin, CurrentSec])`, which should result in strings like "9:45" and "3:06", for example – Dave Nottage May 31 '21 at 03:21

1 Answers1

5

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'

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • @test12345 You do know, don't you, that you can mark correct answers by clicking green the tick mark beside the answer, and optionally upvote / downvote by using the up / down arrow buttons. – Tom Brunberg Jun 07 '21 at 21:56