如何在Delphi中播放wav文件?

23

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


简单。使用MMsystem.PlaySound。像这样使用:PlaySound(pchar(文件名), 1, SND_ASYNC或SND_FILENAME); - Gabriel
5个回答

31

这是最快的方式:

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;

1
sndPlaySound仅用于向后兼容。 - Gabriel
这个过程在FireMonkey中有效吗?如果是,那么它会在其他非Windows平台上工作吗? - Shaun Roselt
SND_LOOP将无限次播放歌曲。 - Soon Santos

7

人们经常引用sndPlaySound,但这已经相当过时了,并且仅为向后兼容而在Delphi中提供。因此,请停止使用它!


话虽如此,应该使用:

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;

要播放预定义的Windows声音,请使用以下方法:

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


For SystemSoundName use one of the constants below:

  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.

所有可用的常量都在注册表中的路径HKEY_CURRENT_USER -> AppEvents -> Schemes -> Apps -> .Default下定义。

(说明:该段内容介绍了在注册表中定义常量的路径)

sndPlaySound - 为什么不使用它?http://www.xtremevbtalk.com/general/35559-difference-sndplaysound-playsound.html - Gabriel
你可以添加MSDN链接:https://msdn.microsoft.com/zh-cn/library/dd743680(v=vs.85).aspx - EMBarbosa
1
那么为什么Embarcadero在他们最新的项目中仍然使用sndPlaySound呢? - Shaun Roselt
@ShaunRoselt-在Delphi中仍存在着大量的遗留代码。但是经过约15年的呼吁,开发人员终于被Embarcadero听到了。看起来他们已经开始修复大量的错误,从Delphi Berlin更新1开始。在此版本之前,他们只是在Delphi中添加错误:) :) - Gabriel
不要使用sndPlaySound。它只是为了兼容性而存在:http://www.xtremevbtalk.com/general/35559-difference-sndplaysound-playsound.html - Gabriel

6
使用WIN32-API(MMSystem单元)中的sndPlaySound函数:
sndPlaySound('C:\ Windows \ Media \ Tada.wav',SND_ASYNC);

sndPlaySound仅存在于向后兼容性。 - Gabriel

4

链接已失效。 - Jeroen Wiert Pluimers
1
很遗憾,但仍然可以通过archive.org访问它:http://web.archive.org/web/20080509074922/http://www.latiumsoftware.com/en/delphi/00024.php - Name
不要使用sndPlaySound,它只是为了兼容性而存在:http://www.xtremevbtalk.com/general/35559-difference-sndplaysound-playsound.html - Gabriel

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接