得,给你一个例子,我刚刚做的,用线程同时放两个mp3文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MPlayer;
type
TInitMethod = procedure(const sFileName: String) of object;
TSkyInitThread = class(TThread)
private
FInitProc: TInitMethod;
FFileName: String;
procedure InnerProc;
protected
procedure Execute;
override;
public
constructor Create(InitProc: TInitMethod;
const sFileName: String);
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure MediaPlayer1Notify(Sender: TObject);
private
{ Private declarations }
proceduredo
Init(const sFileName: String);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TSkyInitThread.Create(InitProc: TInitMethod;
const sFileName: String);
begin
FreeOnTerminate := True;
inherited Create(True);
FInitProc := InitProc;
FFileName := sFileName;
end;
procedure TSkyInitThread.InnerProc;
begin
if Assigned(FInitProc) then
FInitProc(FFileName);
end;
procedure TSkyInitThread.Execute;
begin
Synchronize(InnerProc);
//注意:应用Synchronize()调用!!!
end;
procedure TForm1.DoInit(const sFileName: String);
begin
with TMediaPlayer.Create(Self)do
try
Parent := Self;
Visible := False;
FileName := sFileName;
OnNotify := MediaPlayer1Notify;
//只是测试一下,可不要
Open;
Play;
finally
//Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with TSkyInitThread.Create(DoInit, 'F:/Sky/mp3_A_01/DreamLand-children.mp3')do
begin
Resume();
end;
with TSkyInitThread.Create(DoInit, 'F:/Sky/mp3_A_01/三万英尺(迪克牛仔).mp3')do
begin
Resume();
end;
end;
procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
if Sender is TMediaPlayer then
Sender.Free;
end;
end.