【问题标题】:How to play a wav-File in Delphi?如何在 Delphi 中播放 wav 文件?
【发布时间】:2010-09-19 18:58:15
【问题描述】:

Delphi 中有哪些函数可以播放声音文件?

【问题讨论】:

  • 简单。使用 MMsystem.PlaySound。像这样使用它:PlaySound(pchar(FileName), 1, SND_ASYNC or SND_FILENAME);

标签: windows delphi audio


【解决方案1】:

这是最快的方法:

uses MMSystem;

procedure TForm1.Button1Click(Sender: TObject);
begin
  sndPlaySound('C:\Windows\Media\Tada.wav',
    SND_NODEFAULT Or SND_ASYNC Or SND_LOOP);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  sndPlaySound(nil, 0); // Stops the sound
end;

【讨论】:

  • sndPlaySound 仅用于向后兼容
  • 这个程序在 FireMonkey 中有效吗?如果是,那么它是否可以在非 Windows 的其他平台上运行?
  • SND_LOOP 将无限次播放歌曲
【解决方案2】:

使用来自 WIN32-API(单元 MMSystem)的函数 sndPlaySound:

sndPlaySound('C:\Windows\Media\Tada.wav', SND_ASYNC);

【讨论】:

  • sndPlaySound 仅用于向后兼容
【解决方案3】:

简单:

procedure PlaySoundFile(FileName: string);
begin
 if FileExists(FileName)
 then PlaySound(pchar(FileName), 0, SND_ASYNC or SND_FILENAME);  

 { Flags are:
    SND_SYNC  =0 = Start playing, and wait for the sound to finish
    SND_ASYNC =1 = Start playing, and don't wait to return
    SND_LOOP  =8 = Keep looping the sound until another sound is played  }
end;

人们也引用了 sndPlaySound,但这只是为了向后兼容。 所以,不要使用它!

您可能也对此感兴趣:

procedure PlayWinSound(SystemSoundName: string);
begin
 Winapi.MMSystem.PlaySound(PChar(SystemSoundName), 0, SND_ASYNC);
end;

{ All available constants are defined in the registry under the path HKEY_CURRENT_USER -> AppEvents -> Schemes -> Apps -> .Default. 
  System sounds:
    SystemEXCLAMATION        - Note)
    SystemHAND               - Critical Stop)
    SystemQUESTION           - Question)
    SystemSTART              - Windows-Start)
    SystemEXIT               - Windows-Shutdown)
    SystemASTERIX            - Star)
    RESTOREUP                - Enlarge)
    RESTOREDOWN              - Shrink)
    MENUCOMMAND              - Menu)
    MENUPOPUP                - Pop-Up)
    MAXIMIZE                 - Maximize)
    MINIMIZE                 - Minimize)
    MAILBEEP                 - New Mail)
    OPEN                     - Open Application)
    CLOSE                    - Close Application)
    APPGPFAULT               - Program Error)
    Asterisk                 - played when a popup alert is displayed, like a warning message.
    Calendar Reminder        - played when a Calendar event is taking place.
    Critical Battery Alarm   - played when your battery reaches its critical level.
    Critical Stop            - played when a fatal error occurs.
    Default Beep             - played for multiple reasons, depending on what you do. For example, it will play if you try to select a parent window before closing the active one.
    Desktop Mail Notif       - played when you receive a message in your desktop email client.
    Device Connect           - played when you connect a device to your computer. For example, when you insert a memory stick.
    Device Disconnect        - played when you disconnect a device from your computer.
    Device Connect Failed    - played when something happened with the device that you were trying to connect.
    Exclamation              - played when you try to do something that is not supported by Windows.
    Instant Message Notif    - played when you receive an instant message.
    Low Battery Alarm        - played when the battery is running low.
    Message Nudge            - played when you receive a BUZZ in an instant message.
    New Fax Notification     - played when you receive a fax via your fax-modem.
    New Mail Notification    - played when you receive an email message.
    New Text Message Notif   - played when you receive a text message.
    NFP Completion           - played when the transfer of data via NFC between your Windows device and another device is completed.
    NFP Connection           - played when your Windows device is connecting to another device via NFC.
    Notification             - played when a default notification from a program or app is displayed.
    System Notification      - played when a system notification is displayed.  }

【讨论】:

  • sndPlaySound - 为什么不使用它? xtremevbtalk.com/general/…
  • 那为什么 Embarcadero 还在他们最新的项目中使用 sndPlaySound?
  • @ShaunRoslt-Delphi 中仍有大量遗留代码。但是经过大约 15 年的尖叫开发人员正在听到 Embarcadero 的声音。从 Delphi Berlin 更新 1 开始,他们似乎开始修复大量错误。直到这个版本,他们只在 Delphi 中添加错误 :) :)
  • 不要使用 sndPlaySound。它只是为了兼容性:xtremevbtalk.com/general/…
【解决方案4】:

这个页面很好地解释了如何使用函数 sndPlaySound 以及如何将 wav 文件作为资源嵌入: http://www.latiumsoftware.com/en/delphi/00024.php

【讨论】:

【解决方案5】:

完整教程可在:http://sheepdogguides.com/dt3f.htm

有点老了。但它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多