delphi 播放声音 建议采用 异步方式,比较流畅

2018-10-30

//声明:
sndPlaySound(
lpszSoundName: PChar; {声音文件}
uFlags: UINT{播放选项}
): BOOL;

//uFlags 参数可选值:
SND_SYNC = 0; {同步播放, 程序须等到播放完毕才向下执行}
SND_ASYNC = 1; {异步播放, 在函数返回之后开始播放, 不影响程序继续执行}
SND_NODEFAULT = 2; {声音文件缺失时, 函数自动返回不播放默认声音}
SND_MEMORY = 4; {播放内存中的声音, 譬如资源文件中的声音}
SND_LOOP = 8; {循环播放, 需要和 SND_ASYNC 组合使用}
SND_NOSTOP = 16;{如果当前正在播放声音, 立即返回 False}
--------------------------------------------------------------------------------

//举例:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses MMSystem; {sndPlaySound 声明在该单元}

const
s = 'C:\WINDOWS\Media\Windows XP 启动.wav';

//同步播放
procedure TForm1.Button1Click(Sender: TObject);
begin
sndPlaySound(s, SND_SYNC);
Beep; {播放完毕才会执行这句}
end;

//异步播放
procedure TForm1.Button2Click(Sender: TObject);
begin
sndPlaySound(s, SND_ASYNC);
Beep; {马上会执行这句}
end;

//反复播放
procedure TForm1.Button3Click(Sender: TObject);
begin
sndPlaySound(s, SND_LOOP or SND_ASYNC);
end;

end.
--------------------------------------------------------------------------------

//sndPlaySound 也可以用来播放资源或内存中的声音文件, 参加示例:
阅读42