代码拉取完成,页面将自动刷新
unit SoundEngn;
interface
uses
System.Classes,Winapi.Windows,DXSounds,Mpeg,System.SysUtils;
type
TPlaySound = class(TThread)
m_UserCriticalSection: TRTLCriticalSection;
m_SoundList: TStringList;
m_SoundTempList: TStringList;
m_MediaList: TStringList;
m_MediaTempList: TStringList;
m_sFileName: string;
m_DXSound: TDXSound;
m_Sound: TSoundEngine;
m_boSound: Boolean;
m_MP3: TMPEG;
private
{ Private declarations }
procedure Run;
function GetInitialized: Boolean;
procedure PlayMedia;
protected
procedure Execute; override;
public
constructor Create(AOwner: TComponent);
destructor Destroy; override;
procedure MediaPlayer(const sFileName: string);
procedure PlayMp3(const sFileName: string);
procedure PlayBGM(const sFileName: string);
procedure PlaySound(const sFileName: string);
procedure PlayOnLineSound(const sFileName: string);
procedure Initialize;
procedure Clear;
property Initialized: Boolean read GetInitialized;
end;
implementation
var
boFirstMedia: Boolean = True;
constructor TPlaySound.Create(AOwner: TComponent);
begin
inherited Create(False);
InitializeCriticalSection(m_UserCriticalSection);
m_SoundList := TStringList.Create;
m_SoundTempList := TStringList.Create;
m_MediaList := TStringList.Create;
m_MediaTempList := TStringList.Create;
m_DXSound := TDXSound.Create(AOwner);
m_Sound := nil;
m_MP3 := nil;
m_boSound := True;
// FreeOnTerminate:=True;
end;
destructor TPlaySound.Destroy;
begin
m_SoundList.Free;
m_SoundTempList.Free;
m_MediaList.Free;
m_MediaTempList.Free;
if m_Sound <> nil then m_Sound.Free;
if m_MP3 <> nil then m_MP3.Free;
m_DXSound.Free;
DeleteCriticalSection(m_UserCriticalSection);
inherited Destroy;
end;
procedure TPlaySound.Execute;
//resourcestring
// sExceptionMsg = '[Exception] TPlaySound::Execute';
begin
while not Terminated do begin
try
Run();
except
end;
Sleep(1);
end;
end;
procedure TPlaySound.Initialize;
begin
m_DXSound.Initialize;
if m_DXSound.Initialized then begin
m_Sound := TSoundEngine.Create(m_DXSound.DSound);
m_MP3 := TMPEG.Create(nil);
end;
end;
function TPlaySound.GetInitialized: Boolean;
begin
Result := m_DXSound.Initialized;
end;
procedure TPlaySound.PlaySound(const sFileName: string);
begin
EnterCriticalSection(m_UserCriticalSection);
try
if m_SoundList.Count > 0 then begin
m_SoundList.InsertObject(0, sFileName, TObject(GetTickCount));
end else begin
m_SoundList.AddObject(sFileName, TObject(GetTickCount));
end;
finally
LeaveCriticalSection(m_UserCriticalSection);
end;
end;
procedure TPlaySound.PlayOnLineSound(const sFileName: string);
begin
m_sFileName := sFileName;
{EnterCriticalSection(m_UserCriticalSection);
try
//m_SoundOnLineList.InsertObject(0, sFileName, TObject(GetTickCount));
if m_SoundList.Count > 0 then begin
m_SoundList.InsertObject(0, sFileName, TObject(GetTickCount));
end else begin
m_SoundList.AddObject(sFileName, TObject(GetTickCount));
end;
finally
LeaveCriticalSection(m_UserCriticalSection);
end; }
end;
procedure TPlaySound.MediaPlayer(const sFileName: string);
begin
EnterCriticalSection(m_UserCriticalSection);
try
if m_MediaList.IndexOf(sFileName) < 0 then
m_MediaList.AddObject(sFileName, TObject(GetTickCount));
finally
LeaveCriticalSection(m_UserCriticalSection);
end;
end;
procedure TPlaySound.PlayMedia;
var
I: Integer;
begin
for I := 0 to m_MediaList.Count - 1 do begin
// MediaStartPlay(m_MediaList[I], False, True); //ʹ岥
end;
m_MediaList.Clear;
end;
procedure TPlaySound.Run;
var
I: Integer;
begin
EnterCriticalSection(m_UserCriticalSection);
try
m_SoundTempList.Clear;
m_SoundTempList.AddStrings(m_SoundList);
m_SoundList.Clear;
if (m_Sound <> nil) and m_boSound then begin
for I := 0 to m_SoundTempList.Count - 1 do begin
if (m_SoundTempList[I] <> '') and ((GetTickCount - LongWord(m_SoundTempList.Objects[I])) < 100) then begin
if FileExists(m_SoundTempList[I]) then begin
try
//Sleep(10);
m_Sound.EffectFile(m_SoundTempList[I], False, False);
except
end;
end;
end;
end;
end;
m_MediaTempList.Clear;
m_MediaTempList.AddStrings(m_MediaList);
m_MediaList.Clear;
for I := 0 to m_MediaTempList.Count - 1 do begin
if (m_MediaTempList[I] <> '') then begin
if boFirstMedia then begin
if ((GetTickCount - LongWord(m_MediaTempList.Objects[I])) > 3000) then begin
boFirstMedia := False;
// MediaStartPlay(m_MediaTempList[I], False, True);
end else m_MediaList.AddObject(m_MediaTempList[I], m_MediaTempList.Objects[I]);
end else begin
// MediaStartPlay(m_MediaTempList[I], False, True);
end;
end;
end;
finally
LeaveCriticalSection(m_UserCriticalSection);
end;
end;
procedure TPlaySound.PlayBGM(const sFileName: string);
begin
//if not g_boBGSound then Exit;
if (m_Sound <> nil) and FileExists(sFileName) then begin
// m_Sound.Enabled := g_boBGSound;
try
m_Sound.EffectFile(sFileName, True, False);
except
end;
end;
end;
procedure TPlaySound.Clear;
begin
m_SoundList.Clear;
if m_Sound <> nil then begin
m_Sound.Clear;
end;
end;
procedure TPlaySound.PlayMp3(const sFileName: string);
begin
if m_MP3 <> nil then begin
if sFileName <> '' then
if FileExists(sFileName) then begin
try
m_MP3.stop;
m_MP3.play(sFileName);
except
end;
end;
end;
end;
end.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。