给个EMAIL邮箱,专门做了一个DEMO送给你。
//一个TForm,一个TLabel,三个TButton,一个TMemo,一个TMediaPlayer,一个TTimer
//在当前目录下建个子目录wav,
//子目录wav下放五个声音文件:user1.wav、
user2.wav、
user3.wav、
user4.wav、
user5.wav
//编码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Contnrs, ExtCtrls, MPlayer;
const
WM_COMEON_PLAYMYSOUND = WM_USER + 1122; //自定义消息
MAX_USERS_LIST =5;// 256; //客户要求队列最大容量
type
TUserInfo = record //定义此结构可扩展性增强
SoundID
WORD;
end;
PUserInfo = ^TUserInfo;
type
TForm1 = class(TForm)
Memo1: TMemo;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Timer1: TTimer;
MediaPlayer1: TMediaPlayer;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
FRequestQueue:TQueue;
FPreviousSoundEndPlaying:Boolean;
procedure WMCOMEONPLAYMYSOUND(var msg:TMessage);message WM_COMEON_PLAYMYSOUND;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.WMCOMEONPLAYMYSOUND(var msg:TMessage); //自定义消息处理
var
P
UserInfo;
begin
if FRequestQueue.Count < MAX_USERS_LIST then
begin
New(P);
P^.SoundID := msg.wParam;
FRequestQueue.Push( P );
Memo1.Lines.Add('Client request has been push into the Queue
-> [ Client No.'+IntToStr(msg.wParam)+' ]');
end
else
memo1.Lines.Add('Users list is full, message''s been ignored!');
end;
procedure TForm1.Button3Click(Sender: TObject); //模拟客户请求
var
I,J:Integer;
begin
for J:=0 to 9 do
begin
I:=random(5); if I = 0 then I:=1;
SendMessage(Handle,WM_COMEON_PLAYMYSOUND,I,0);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Clear;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FRequestQueue:=TQueue.Create;
FPreviousSoundEndPlaying:=True; //初始化为前一个播放已经到结尾,即可以播放下一个
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FRequestQueue.Free;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
FNPlaySound:TFileName;
SID
WORD;
P
UserInfo;
begin
if FPreviousSoundEndPlaying then
begin
if FRequestQueue.Count > 0 then
begin
FPreviousSoundEndPlaying:=False;
///////////////////////////////////////////////////////////////////////
P:=FRequestQueue.Pop; //按顺序播放客户请求
SID:=P^.SoundID;
Dispose(P);
///////////////////////////////////////////////////////////////////////
FNPlaySound:=ExtractFilePath(Application.ExeName )+'wav/user'+IntToStr(SID)+'.wav';
with MediaPlayer1 do
begin
FileName:=FNPlaySound;
Open;
Play;
Memo1.Lines.Add('Playing '+FileName+' ...');
end;
end;
end
else
begin
with MediaPlayer1 do
begin
FPreviousSoundEndPlaying:= Position = Length;
if FPreviousSoundEndPlaying then
begin
Close;
Memo1.Lines.Add('Stop.');
Memo1.Lines.Add('==========================================');
end;
end;
end;
end;
end.