获取 wav 音频的长度(以分钟/秒为单位)

发布于 2024-10-30 20:48:30 字数 1192 浏览 5 评论 0原文

使用 DelphiXE,我试图在标签上显示 wav 文件的长度。这是加载到 tMediaPlayer 中的固定比特率 64kbps 的 wav 文件。

之前关于该任务的 SO 帖子位于此处。但没有显示任何代码,并且 Devhood 的链接似乎不再有效,因此我无法尝试该方法。

我还尝试了 HERE 中的代码,但它给出了错误结果如下。

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;

此代码给出的值为 24:23:4,而它应该是 0:04:28。

该代码是否存在明显的问题,或者是否有一些更优雅的方法来完成此任务?

一如既往,感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2024-11-06 20:48:30

为什么不做一些简单的小学数学呢?

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]);

但为什么HMS不起作用呢?好吧,根据官方文档

MCI_FORMAT_HMS

更改时间格式
到小时、分钟和秒。
被录像机和视频光盘识别
设备类型。

MCI_FORMAT_MILLISECONDS

改变
时间格式为毫秒。
所有设备类型均可识别。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文