- 播放 wav 文件

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、PlaySound 都是在此单元声明的}

procedure TForm1.Button1Click(Sender: TObject);
var
  rs: TResourceStream;
begin
  rs := TResourceStream.Create(HInstance, 'sound1' , 'WAVE' );
  sndPlaySound(rs.Memory, SND_ASYNC or SND_MEMORY);
  rs.Free;
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.
//////////////////////////////////////////////////////////////////////////
//rc 文件:
sound1 WAVE "SoundFile1.wav"
sound2 WAVE "SoundFile2.wav"



//代码:
unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses MMSystem; {下面用到的 sndPlaySound、PlaySound 都是在此单元声明的}

procedure TForm1.Button1Click(Sender: TObject);
var
  rs: TResourceStream;
begin
  rs := TResourceStream.Create(HInstance, 'sound1' , 'WAVE' );
  sndPlaySound(rs.Memory, SND_ASYNC or SND_MEMORY);
  rs.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
  rs: TResourceStream;
begin
  rs := TResourceStream.Create(HInstance, 'sound2' , 'WAVE' );
  PlaySound(rs.Memory, HInstance, SND_ASYNC or SND_MEMORY);
  rs.Free;
end;

end.

你可能感兴趣的:(文件)