1

Using DelphiXE, I'm trying to show the length of a wav file on a label. This is a wav file at fixed bit rate of 64kbps that is loaded into a tMediaPlayer.

A previous SO post on the task is HERE. But no code is shown and the link to Devhood no longer appears to work so I was unable to try that method.

I also tried the code from HERE but it gives incorrect results as follows.

type

  HMSRec = record
    Hours: byte;
    Minutes: byte;
    Seconds: byte;
    NotUsed: byte;

  end;

procedure TForm1.Button1Click(Sender: TObject);

var
  TheLength: LongInt;
begin

  { Set time format - note that some devices don’t support tfHMS }

  MediaPlayer1.TimeFormat := tfHMS;
  { Store length of currently loaded media }
  TheLength := MediaPlayer1.Length;
  with HMSRec(TheLength) do { Typecast TheLength as a HMSRec record }
  begin
    Label1.Caption := IntToStr(Hours); { Display Hours in Label1 }
    Label2.Caption := IntToStr(Minutes); { Display Minutes in Label2 }
    Label3.Caption := IntToStr(Seconds); { Display Seconds in Label3 }
  end;
end;

This code gives a value of 24:23:4, when it should be 0:04:28.

Is there an obvious problem with that code, or is there some more elegant way to accomplish this?

As always, thanks for your help.

Community
  • 1
  • 1
Bobby
  • 81
  • 2
  • 8

1 Answers1

3

Why not just do some simple elementary-school math?

var
  sec,
  min,
  hr: integer;
begin
  MediaPlayer1.TimeFormat := tfMilliseconds;
  sec := MediaPlayer1.Length div 1000;
  hr := sec div SecsPerHour;
  min := (sec - (hr * SecsPerHour)) div SecsPerMin;
  sec := sec - hr * SecsPerHour - min * SecsPerMin;
  Caption := Format('%d hours, %d minutes, and %d seconds', [hr, min, sec]);

But why don't HMS work? Well, according to the official documentation:

MCI_FORMAT_HMS

Changes the time format to hours, minutes, and seconds. Recognized by the vcr and videodisc device types.

MCI_FORMAT_MILLISECONDS

Changes the time format to milliseconds. Recognized by all device types.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • Works for me! I guess it's just a case where sometimes the simplest answer didn't occur. Thank you once again Andreas! – Bobby Apr 05 '11 at 18:05