playsoundpackage: More flexible usage of PlayCommand (empty string = builtin command). Improve demo.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5016 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2016-07-20 20:08:46 +00:00
parent 46e048dc53
commit 0299f6dba7
3 changed files with 39 additions and 20 deletions

View File

@ -10,7 +10,7 @@ object mainform: Tmainform
DefaultMonitor = dmPrimary DefaultMonitor = dmPrimary
OnCreate = FormCreate OnCreate = FormCreate
Position = poScreenCenter Position = poScreenCenter
LCLVersion = '1.6.0.1' LCLVersion = '1.7'
object cmd_Async: TButton object cmd_Async: TButton
Left = 264 Left = 264
Height = 25 Height = 25
@ -52,20 +52,20 @@ object mainform: Tmainform
TIObject = playsound1 TIObject = playsound1
ValueFont.Color = clMaroon ValueFont.Color = clMaroon
end end
object cmd_StpSound: TButton object cmd_StopSound: TButton
Left = 264 Left = 264
Height = 25 Height = 25
Top = 112 Top = 112
Width = 75 Width = 75
Caption = 'Stop Sound' Caption = 'Stop Sound'
OnClick = cmd_StpSoundClick OnClick = cmd_StopSoundClick
TabOrder = 4 TabOrder = 4
end end
object playsound1: Tplaysound object playsound1: Tplaysound
About.Description.Strings = ( About.Description.Strings = (
'Plays WAVE sounds in Windows or Linux' 'Plays WAVE sounds in Windows or Linux'
) )
About.Title = 'About About About PlaySound' About.Title = 'About About About About About About PlaySound'
About.Height = 400 About.Height = 400
About.Width = 400 About.Width = 400
About.Font.Color = clNavy About.Font.Color = clNavy
@ -77,9 +77,7 @@ object mainform: Tmainform
About.AuthorEmail = 'minesadorada@charcodelvalle.com' About.AuthorEmail = 'minesadorada@charcodelvalle.com'
About.ComponentName = 'PlaySound' About.ComponentName = 'PlaySound'
About.LicenseType = abLGPL About.LicenseType = abLGPL
SoundFile = '/home/gordon/development/lazarus/' left = 104
PlayCommand = 'sndPlaySnd' top = 40
left = 4
top = 8
end end
end end

View File

@ -6,7 +6,7 @@ interface
uses uses
Classes, SysUtils, FileUtil, RTTIGrids, Forms, Controls, Graphics, Dialogs, Classes, SysUtils, FileUtil, RTTIGrids, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, uplaysound; StdCtrls, Buttons, ExtCtrls, uplaysound;
type type
@ -14,13 +14,13 @@ type
Tmainform = class(TForm) Tmainform = class(TForm)
BitBtn1: TBitBtn; BitBtn1: TBitBtn;
cmd_StpSound: TButton; cmd_StopSound: TButton;
cmd_Async: TButton; cmd_Async: TButton;
cmd_Sync: TButton; cmd_Sync: TButton;
playsound1: Tplaysound; playsound1: Tplaysound;
TIPropertyGrid1: TTIPropertyGrid; TIPropertyGrid1: TTIPropertyGrid;
procedure cmd_AsyncClick(Sender: TObject); procedure cmd_AsyncClick(Sender: TObject);
procedure cmd_StpSoundClick(Sender: TObject); procedure cmd_StopSoundClick(Sender: TObject);
procedure cmd_SyncClick(Sender: TObject); procedure cmd_SyncClick(Sender: TObject);
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
private private
@ -41,26 +41,35 @@ implementation
procedure Tmainform.cmd_AsyncClick(Sender: TObject); procedure Tmainform.cmd_AsyncClick(Sender: TObject);
// No gap between sounds. App remains responsive // No gap between sounds. App remains responsive
begin begin
{$IFDEF WINDOWS}
playsound1.PlayStyle:=psASync; playsound1.PlayStyle:=psASync;
playsound1.SoundFile:='doorbell.wav'; playsound1.SoundFile:='doorbell.wav';
playsound1.Execute; playsound1.Execute;
playsound1.SoundFile:='telephone.wav'; playsound1.SoundFile:='telephone.wav';
playsound1.Execute; playsound1.Execute;
{$ELSE}
// Sound file taken from PropertyGrid
playsound1.Execute;
{$ENDIF}
end; end;
procedure Tmainform.cmd_StpSoundClick(Sender: TObject); procedure Tmainform.cmd_StopSoundClick(Sender: TObject);
begin begin
playsound1.StopSound; playsound1.StopSound;
end; end;
procedure Tmainform.cmd_SyncClick(Sender: TObject); procedure Tmainform.cmd_SyncClick(Sender: TObject);
begin begin
{$IFDEF WINDOWS}
playsound1.PlayStyle:=psSync; playsound1.PlayStyle:=psSync;
playsound1.SoundFile:='doorbell.wav'; playsound1.SoundFile:='doorbell.wav';
playsound1.Execute; playsound1.Execute;
playsound1.SoundFile:='telephone.wav'; playsound1.SoundFile:='telephone.wav';
playsound1.Execute; playsound1.Execute;
{$ELSE}
// Sound file taken from PropertyGrid
playsound1.Execute;
{$ENDIF}
end; end;
procedure Tmainform.FormCreate(Sender: TObject); procedure Tmainform.FormCreate(Sender: TObject);

View File

@ -19,10 +19,12 @@ type
SoundPlayerSyncProcess: Tprocess; SoundPlayerSyncProcess: Tprocess;
{$ENDIF} {$ENDIF}
fPlayCommand:String; fPlayCommand:String;
fDefaultPlayCommand: String;
fPathToSoundFile: string; fPathToSoundFile: string;
fPlayStyle: TPlayStyle; fPlayStyle: TPlayStyle;
protected protected
{ Protected declarations } { Protected declarations }
function GetPlayCommand: String;
procedure PlaySound(const szSoundFilename: string); virtual; procedure PlaySound(const szSoundFilename: string); virtual;
public public
{ Public declarations } { Public declarations }
@ -100,11 +102,11 @@ constructor Tplaysound.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
fPlayStyle := psASync; fPlayStyle := psASync;
fPathToSoundFile := ProgramDirectory; // fPathToSoundFile := ProgramDirectory;
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
fPlayCommand:='sndPlaySnd'; fDefaultPlayCommand := 'sndPlaySound';
{$ELSE} {$ELSE}
fPlayCommand:=GetNonWindowsPlayCommand; // Linux, Mac etc. fDefaultPlayCommand := GetNonWindowsPlayCommand; // Linux, Mac etc.
{$ENDIF} {$ENDIF}
// About Dialog properties // About Dialog properties
AboutBoxComponentName := 'PlaySound'; AboutBoxComponentName := 'PlaySound';
@ -143,6 +145,14 @@ begin
end; end;
end; end;
function TPlaySound.GetPlayCommand: String;
begin
if FPlayCommand = '' then
Result := FDefaultPlayCommand
else
Result := FPlayCommand;
end;
procedure Tplaysound.PlaySound(const szSoundFilename: string); procedure Tplaysound.PlaySound(const szSoundFilename: string);
var var
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
@ -150,6 +160,7 @@ var
{$ELSE} {$ELSE}
L: TStrings; L: TStrings;
i: Integer; i: Integer;
playCmd: String;
{$ENDIF} {$ENDIF}
begin begin
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
@ -166,16 +177,17 @@ begin
// How to play in Linux? Use generic Linux commands // How to play in Linux? Use generic Linux commands
// Use asyncprocess to play sound as SND_ASYNC // Use asyncprocess to play sound as SND_ASYNC
// proceed if we managed to find a valid command // proceed if we managed to find a valid command
if (fPlayCommand <> '') then playCmd := GetPlayCommand;
if (playCmd <> '') then
begin begin
L := TStringList.Create; L := TStringList.Create;
try try
L.Delimiter := ' '; L.Delimiter := ' ';
L.DelimitedText := fPlayCommand; L.DelimitedText := playCmd;
if fPlayStyle = psASync then if fPlayStyle = psASync then
begin begin
if SoundPlayerAsyncProcess = nil then if SoundPlayerAsyncProcess = nil then
SoundPlayerAsyncProcess := Tasyncprocess.Create(nil); SoundPlayerAsyncProcess := TaSyncProcess.Create(nil);
SoundPlayerAsyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename); SoundPlayerAsyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename);
SoundPlayerAsyncProcess.Executable := FindDefaultExecutablePath(L[0]); SoundPlayerAsyncProcess.Executable := FindDefaultExecutablePath(L[0]);
SoundPlayerAsyncProcess.Parameters.Clear; SoundPlayerAsyncProcess.Parameters.Clear;
@ -193,7 +205,7 @@ begin
else else
begin begin
if SoundPlayerSyncProcess = nil then if SoundPlayerSyncProcess = nil then
SoundPlayerSyncProcess := Tprocess.Create(nil); SoundPlayerSyncProcess := TProcess.Create(nil);
SoundPlayerSyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename); SoundPlayerSyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename);
SoundPlayerSyncProcess.Executable := FindDefaultExecutablePath(L[0]); SoundPlayerSyncProcess.Executable := FindDefaultExecutablePath(L[0]);
SoundPlayersyncProcess.Parameters.Clear; SoundPlayersyncProcess.Parameters.Clear;