开发者

get length of wav audio in minutes/seconds

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜