V0.0.3 PlayCommand property added

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3562 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
gbamber
2014-09-14 15:04:10 +00:00
parent 432cfcc6c0
commit 3811b1ef1e
6 changed files with 105 additions and 74 deletions

View File

@@ -1,4 +1,4 @@
<?xml version="1.0"?> <?xml version="1.0" encoding="UTF-8"?>
<CONFIG> <CONFIG>
<ProjectOptions> <ProjectOptions>
<Version Value="9"/> <Version Value="9"/>
@@ -31,17 +31,20 @@
<FormatVersion Value="1"/> <FormatVersion Value="1"/>
</local> </local>
</RunParams> </RunParams>
<RequiredPackages Count="3"> <RequiredPackages Count="4">
<Item1> <Item1>
<PackageName Value="poweredby"/> <PackageName Value="RunTimeTypeInfoControls"/>
</Item1> </Item1>
<Item2> <Item2>
<PackageName Value="playwavepackage"/> <PackageName Value="poweredby"/>
<MaxVersion Release="1"/>
</Item2> </Item2>
<Item3> <Item3>
<PackageName Value="LCL"/> <PackageName Value="playwavepackage"/>
<MaxVersion Release="1"/>
</Item3> </Item3>
<Item4>
<PackageName Value="LCL"/>
</Item4>
</RequiredPackages> </RequiredPackages>
<Units Count="2"> <Units Count="2">
<Unit0> <Unit0>

View File

@@ -7,7 +7,7 @@ uses
cthreads, cthreads,
{$ENDIF}{$ENDIF} {$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset Interfaces, // this includes the LCL widgetset
Forms, poweredby, umainform Forms, runtimetypeinfocontrols, poweredby, umainform
{ you can add units after this }; { you can add units after this };
{$R *.res} {$R *.res}

View File

@@ -1,29 +1,29 @@
object mainform: Tmainform object mainform: Tmainform
Left = 415 Left = 415
Height = 129 Height = 257
Top = 305 Top = 305
Width = 241 Width = 352
BorderIcons = [biSystemMenu] BorderIcons = [biSystemMenu]
Caption = 'mainform' Caption = 'mainform'
ClientHeight = 129 ClientHeight = 257
ClientWidth = 241 ClientWidth = 352
DefaultMonitor = dmPrimary DefaultMonitor = dmPrimary
OnCreate = FormCreate OnCreate = FormCreate
Position = poScreenCenter Position = poScreenCenter
LCLVersion = '1.1' LCLVersion = '1.2.4.0'
object cmd_Async: TButton object cmd_Async: TButton
Left = 16 Left = 264
Height = 25 Height = 25
Top = 16 Top = 180
Width = 75 Width = 75
Caption = 'Play Async' Caption = 'Play Async'
OnClick = cmd_AsyncClick OnClick = cmd_AsyncClick
TabOrder = 0 TabOrder = 0
end end
object BitBtn1: TBitBtn object BitBtn1: TBitBtn
Left = 152 Left = 264
Height = 30 Height = 30
Top = 88 Top = 216
Width = 75 Width = 75
DefaultCaption = True DefaultCaption = True
Kind = bkClose Kind = bkClose
@@ -31,19 +31,31 @@ object mainform: Tmainform
TabOrder = 1 TabOrder = 1
end end
object cmd_Sync: TButton object cmd_Sync: TButton
Left = 152 Left = 264
Height = 25 Height = 25
Top = 16 Top = 148
Width = 75 Width = 75
Caption = 'Play Sync' Caption = 'Play Sync'
OnClick = cmd_SyncClick OnClick = cmd_SyncClick
TabOrder = 2 TabOrder = 2
end end
object TIPropertyGrid1: TTIPropertyGrid
Left = 4
Height = 238
Top = 8
Width = 252
DefaultValueFont.Color = clWindowText
Filter = [tkInteger, tkChar, tkEnumeration, tkFloat, tkSet, tkMethod, tkSString, tkLString, tkAString, tkWString, tkVariant, tkArray, tkRecord, tkInterface, tkClass, tkObject, tkWChar, tkBool, tkInt64, tkQWord, tkDynArray, tkInterfaceRaw, tkProcVar, tkUString, tkUChar, tkHelper]
Indent = 16
NameFont.Color = clWindowText
TIObject = playsound1
ValueFont.Color = clMaroon
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 PlaySound' About.Title = 'About About PlaySound'
About.Height = 400 About.Height = 400
About.Width = 400 About.Width = 400
About.Font.Color = clNavy About.Font.Color = clNavy
@@ -56,7 +68,8 @@ object mainform: Tmainform
About.ComponentName = 'PlaySound' About.ComponentName = 'PlaySound'
About.LicenseType = abLGPL About.LicenseType = abLGPL
SoundFile = '/home/gordon/development/lazarus/' SoundFile = '/home/gordon/development/lazarus/'
left = 91 PlayCommand = 'sndPlaySnd'
top = 53 left = 4
top = 8
end end
end end

View File

@@ -5,7 +5,7 @@ unit umainform;
interface interface
uses uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Classes, SysUtils, FileUtil, RTTIGrids, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, uplaysound; StdCtrls, Buttons, uplaysound;
type type
@@ -17,6 +17,7 @@ type
cmd_Async: TButton; cmd_Async: TButton;
cmd_Sync: TButton; cmd_Sync: TButton;
playsound1: Tplaysound; playsound1: Tplaysound;
TIPropertyGrid1: TTIPropertyGrid;
procedure cmd_AsyncClick(Sender: TObject); procedure cmd_AsyncClick(Sender: TObject);
procedure cmd_SyncClick(Sender: TObject); procedure cmd_SyncClick(Sender: TObject);
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);

View File

@@ -1,4 +1,4 @@
<?xml version="1.0"?> <?xml version="1.0" encoding="UTF-8"?>
<CONFIG> <CONFIG>
<Package Version="4"> <Package Version="4">
<Name Value="playwavepackage"/> <Name Value="playwavepackage"/>
@@ -22,7 +22,7 @@
</CompilerOptions> </CompilerOptions>
<Description Value="Simple component to play wave files under Windows or Linux"/> <Description Value="Simple component to play wave files under Windows or Linux"/>
<License Value="LGPLv2"/> <License Value="LGPLv2"/>
<Version Release="1"/> <Version Release="3"/>
<Files Count="2"> <Files Count="2">
<Item1> <Item1>
<Filename Value="uplaysound.pas"/> <Filename Value="uplaysound.pas"/>

View File

@@ -14,10 +14,11 @@ type
Tplaysound = class(TAboutPlaySound) Tplaysound = class(TAboutPlaySound)
private private
{ Private declarations } { Private declarations }
{$IFDEF LINUX} {$IFNDEF WINDOWS}
SoundPlayerAsyncProcess: Tasyncprocess; SoundPlayerAsyncProcess: Tasyncprocess;
SoundPlayerSyncProcess: Tprocess; SoundPlayerSyncProcess: Tprocess;
{$ENDIF} {$ENDIF}
fPlayCommand:String;
fPathToSoundFile: string; fPathToSoundFile: string;
fPlayStyle: TPlayStyle; fPlayStyle: TPlayStyle;
protected protected
@@ -32,13 +33,14 @@ type
{ Published declarations } { Published declarations }
property SoundFile: string read fPathToSoundFile write fPathToSoundFile; property SoundFile: string read fPathToSoundFile write fPathToSoundFile;
property PlayStyle: TPlayStyle read fPlayStyle write fPlayStyle default psASync; property PlayStyle: TPlayStyle read fPlayStyle write fPlayStyle default psASync;
Property PlayCommand:String read fPlayCommand write fPlayCommand;
end; end;
procedure Register; procedure Register;
implementation implementation
{$IFDEF LINUX} {$IFNDEF WINDOWS}
const // Defined in mmsystem const // Defined in mmsystem
SND_SYNC = 0; SND_SYNC = 0;
SND_ASYNC = 1; SND_ASYNC = 1;
@@ -47,62 +49,11 @@ const // Defined in mmsystem
resourcestring resourcestring
C_UnableToPlay = 'Unable to play '; C_UnableToPlay = 'Unable to play ';
constructor Tplaysound.Create(AOwner: TComponent); function GetNonWindowsPlayCommand:String;
begin Var szNonWindowsPlayCommand: string;
inherited Create(AOwner);
fPlayStyle := psASync;
fPathToSoundFile := ProgramDirectory;
// About Dialog properties
AboutBoxComponentName := 'PlaySound';
AboutBoxWidth := 400;
AboutBoxHeight := 400;
AboutBoxBackgroundColor := clCream;
//AboutBoxFontName (string)
//AboutBoxFontSize (integer)
AboutBoxVersion := '0.0.2';
AboutBoxAuthorname := 'Gordon Bamber';
AboutBoxOrganisation := 'Public Domain';
AboutBoxAuthorEmail := 'minesadorada@charcodelvalle.com';
AboutBoxLicenseType := 'LGPL';
AboutBoxDescription := 'Plays WAVE sounds in Windows or Linux';
end;
destructor Tplaysound.Destroy;
begin
{$IFDEF LINUX}
FreeAndNil(SoundPlayerSyncProcess);
FreeAndNil(SoundPlayerAsyncProcess);
{$ENDIF}
inherited;
end;
procedure Tplaysound.Execute;
begin
if not FileExistsUTF8(fPathToSoundFile) then
Exit;
PlaySound(fPathToSoundFile);
end;
procedure Tplaysound.PlaySound(const szSoundFilename: string);
var
flags: word;
szNonWindowsPlayCommand: string;
begin begin
szNonWindowsPlayCommand:=''; szNonWindowsPlayCommand:='';
{$IFDEF WINDOWS} {$IFNDEF WINDOWS}
if fPlayStyle = psASync then
flags := SND_ASYNC or SND_NODEFAULT
else
flags := SND_SYNC or SND_NODEFAULT;
try
sndPlaySound(PChar(szSoundFilename), flags);
except
ShowMessage(C_UnableToPlay + szSoundFilename);
end;
{$ELSE}
// How to play in Linux? Use generic Linux commands
// Use asyncprocess to play sound as SND_ASYNC
// Try play // Try play
if (FindDefaultExecutablePath('play') <> '') then if (FindDefaultExecutablePath('play') <> '') then
szNonWindowsPlayCommand := 'play'; szNonWindowsPlayCommand := 'play';
@@ -142,8 +93,71 @@ begin
if (szNonWindowsPlayCommand = '') then if (szNonWindowsPlayCommand = '') then
if (FindDefaultExecutablePath('afplay') <> '') then if (FindDefaultExecutablePath('afplay') <> '') then
szNonWindowsPlayCommand := 'afplay'; szNonWindowsPlayCommand := 'afplay';
{$ENDIF}
Result:=szNonWindowsPlayCommand;
end;
constructor Tplaysound.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fPlayStyle := psASync;
fPathToSoundFile := ProgramDirectory;
{$IFDEF WINDOWS}
fPlayCommand:='sndPlaySnd';
{$ELSE}
fPlayCommand:=GetNonWindowsPlayCommand;
{$ENDIF}
// About Dialog properties
AboutBoxComponentName := 'PlaySound';
AboutBoxWidth := 400;
AboutBoxHeight := 400;
AboutBoxBackgroundColor := clCream;
//AboutBoxFontName (string)
//AboutBoxFontSize (integer)
AboutBoxVersion := '0.0.3';
AboutBoxAuthorname := 'Gordon Bamber';
AboutBoxOrganisation := 'Public Domain';
AboutBoxAuthorEmail := 'minesadorada@charcodelvalle.com';
AboutBoxLicenseType := 'LGPL';
AboutBoxDescription := 'Plays WAVE sounds in Windows or Linux';
end;
destructor Tplaysound.Destroy;
begin
{$IFNDEF WINDOWS}
FreeAndNil(SoundPlayerSyncProcess);
FreeAndNil(SoundPlayerAsyncProcess);
{$ENDIF}
inherited;
end;
procedure Tplaysound.Execute;
begin
if not FileExistsUTF8(fPathToSoundFile) then
Exit;
PlaySound(fPathToSoundFile);
end;
procedure Tplaysound.PlaySound(const szSoundFilename: string);
var
flags: word;
begin
{$IFDEF WINDOWS}
if fPlayStyle = psASync then
flags := SND_ASYNC or SND_NODEFAULT
else
flags := SND_SYNC or SND_NODEFAULT;
try
sndPlaySound(PChar(szSoundFilename), flags);
except
ShowMessage(C_UnableToPlay + szSoundFilename);
end;
{$ELSE}
// How to play in Linux? Use generic Linux commands
// 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 (szNonWindowsPlayCommand <> '') then if (fNonWindowsPlayCommand <> '') then
begin begin
if fPlayStyle = psASync then if fPlayStyle = psASync then
begin begin
@@ -151,7 +165,7 @@ begin
SoundPlayerAsyncProcess := Tasyncprocess.Create(nil); SoundPlayerAsyncProcess := Tasyncprocess.Create(nil);
SoundPlayerAsyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename); SoundPlayerAsyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename);
SoundPlayerAsyncProcess.Executable := SoundPlayerAsyncProcess.Executable :=
FindDefaultExecutablePath(szNonWindowsPlayCommand); FindDefaultExecutablePath(fNonWindowsPlayCommand);
SoundPlayerAsyncProcess.Parameters.Clear; SoundPlayerAsyncProcess.Parameters.Clear;
SoundPlayerAsyncProcess.Parameters.Add(szSoundFilename); SoundPlayerAsyncProcess.Parameters.Add(szSoundFilename);
try try
@@ -168,7 +182,7 @@ begin
SoundPlayerSyncProcess := Tprocess.Create(nil); SoundPlayerSyncProcess := Tprocess.Create(nil);
SoundPlayerSyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename); SoundPlayerSyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename);
SoundPlayerSyncProcess.Executable := SoundPlayerSyncProcess.Executable :=
FindDefaultExecutablePath(szNonWindowsPlayCommand); FindDefaultExecutablePath(fNonWindowsPlayCommand);
SoundPlayersyncProcess.Parameters.Clear; SoundPlayersyncProcess.Parameters.Clear;
SoundPlayerSyncProcess.Parameters.Add(szSoundFilename); SoundPlayerSyncProcess.Parameters.Add(szSoundFilename);
try try
@@ -183,7 +197,7 @@ begin
end end
else else
raise Exception.CreateFmt('The play command %s does not work on your system', raise Exception.CreateFmt('The play command %s does not work on your system',
[szNonWindowsPlayCommand]); [fNonWindowsPlayCommand]);
{$ENDIF} {$ENDIF}
end; end;