playsoundpackage: Fix operation on non-Windows systems. Fix compilation with Laz 1.6.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5014 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2016-07-20 19:43:49 +00:00
parent b8b487e621
commit 1356ce8029

View File

@ -45,59 +45,54 @@ procedure Register;
implementation implementation
{$IFNDEF WINDOWS} uses
const // Defined in mmsystem LazFileUtils;
SND_SYNC = 0;
SND_ASYNC = 1;
SND_NODEFAULT = 2;
{$ENDIF}
resourcestring resourcestring
C_UnableToPlay = 'Unable to play '; C_UnableToPlay = 'Unable to play ';
function GetNonWindowsPlayCommand:String; function GetNonWindowsPlayCommand:String;
Var szNonWindowsPlayCommand: string;
begin begin
szNonWindowsPlayCommand:=''; Result := '';
// Try play // Try play
if (FindDefaultExecutablePath('play') <> '') then if (FindDefaultExecutablePath('play') <> '') then
szNonWindowsPlayCommand := 'play'; Result := 'play';
// Try aplay // Try aplay
if (szNonWindowsPlayCommand = '') then if (result = '') then
if (FindDefaultExecutablePath('aplay') <> '') then if (FindDefaultExecutablePath('aplay') <> '') then
szNonWindowsPlayCommand := 'aplay -q'; Result := 'aplay -q';
// Try paplay // Try paplay
if (szNonWindowsPlayCommand = '') then if (Result = '') then
if (FindDefaultExecutablePath('paplay') <> '') then if (FindDefaultExecutablePath('paplay') <> '') then
szNonWindowsPlayCommand := 'paplay'; Result := 'paplay';
// Try mplayer // Try mplayer
if (szNonWindowsPlayCommand = '') then if (Result = '') then
if (FindDefaultExecutablePath('mplayer') <> '') then if (FindDefaultExecutablePath('mplayer') <> '') then
szNonWindowsPlayCommand := 'mplayer -really-quiet'; Result := 'mplayer -really-quiet';
// Try CMus // Try CMus
if (szNonWindowsPlayCommand = '') then if (Result = '') then
if (FindDefaultExecutablePath('CMus') <> '') then if (FindDefaultExecutablePath('CMus') <> '') then
szNonWindowsPlayCommand := 'CMus'; Result := 'CMus';
// Try pacat // Try pacat
if (szNonWindowsPlayCommand = '') then if (Result = '') then
if (FindDefaultExecutablePath('pacat') <> '') then if (FindDefaultExecutablePath('pacat') <> '') then
szNonWindowsPlayCommand := 'pacat -p'; Result := 'pacat -p';
// Try ffplay // Try ffplay
if (szNonWindowsPlayCommand = '') then if (Result = '') then
if (FindDefaultExecutablePath('ffplay') <> '') then if (FindDefaultExecutablePath('ffplay') <> '') then
szNonWindowsPlayCommand := 'ffplay -autoexit -nodisp'; result := 'ffplay -autoexit -nodisp';
// Try cvlc // Try cvlc
if (szNonWindowsPlayCommand = '') then if (Result = '') then
if (FindDefaultExecutablePath('cvlc') <> '') then if (FindDefaultExecutablePath('cvlc') <> '') then
szNonWindowsPlayCommand := 'cvlc -q --play-and-exit'; result := 'cvlc -q --play-and-exit';
// Try canberra-gtk-play // Try canberra-gtk-play
if (szNonWindowsPlayCommand = '') then if (Result = '') then
if (FindDefaultExecutablePath('canberra-gtk-play') <> '') then if (FindDefaultExecutablePath('canberra-gtk-play') <> '') then
szNonWindowsPlayCommand := 'canberra-gtk-play -c never -f'; Result := 'canberra-gtk-play -c never -f';
// Try Macintosh command? // Try Macintosh command?
if (szNonWindowsPlayCommand = '') then if (Result = '') then
if (FindDefaultExecutablePath('afplay') <> '') then if (FindDefaultExecutablePath('afplay') <> '') then
szNonWindowsPlayCommand := 'afplay'; Result := 'afplay';
Result:=szNonWindowsPlayCommand;
end; end;
@ -150,7 +145,12 @@ end;
procedure Tplaysound.PlaySound(const szSoundFilename: string); procedure Tplaysound.PlaySound(const szSoundFilename: string);
var var
{$IFDEF WINDOWS}
flags: word; flags: word;
{$ELSE}
L: TStrings;
i: Integer;
{$ENDIF}
begin begin
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
if fPlayStyle = psASync then if fPlayStyle = psASync then
@ -168,14 +168,19 @@ begin
// proceed if we managed to find a valid command // proceed if we managed to find a valid command
if (fPlayCommand <> '') then if (fPlayCommand <> '') then
begin begin
L := TStringList.Create;
try
L.Delimiter := ' ';
L.DelimitedText := fPlayCommand;
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 := SoundPlayerAsyncProcess.Executable := FindDefaultExecutablePath(L[0]);
FindDefaultExecutablePath(fPlayCommand);
SoundPlayerAsyncProcess.Parameters.Clear; SoundPlayerAsyncProcess.Parameters.Clear;
for i := 1 to L.Count-1 do
SoundPlayerAsyncProcess.Parameters.Add(L[i]);
SoundPlayerAsyncProcess.Parameters.Add(szSoundFilename); SoundPlayerAsyncProcess.Parameters.Add(szSoundFilename);
try try
SoundPlayerAsyncProcess.Execute; SoundPlayerAsyncProcess.Execute;
@ -190,9 +195,10 @@ 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 := SoundPlayerSyncProcess.Executable := FindDefaultExecutablePath(L[0]);
FindDefaultExecutablePath(fPlayCommand);
SoundPlayersyncProcess.Parameters.Clear; SoundPlayersyncProcess.Parameters.Clear;
for i:=1 to L.Count-1 do
SoundPlayerSyncProcess.Parameters.Add(L[i]);
SoundPlayerSyncProcess.Parameters.Add(szSoundFilename); SoundPlayerSyncProcess.Parameters.Add(szSoundFilename);
try try
SoundPlayerSyncProcess.Execute; SoundPlayerSyncProcess.Execute;
@ -203,19 +209,23 @@ begin
'%s Message:%s', [szSoundFilename, E.Message]); '%s Message:%s', [szSoundFilename, E.Message]);
end; end;
end; end;
finally
L.Free;
end;
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',
[fPlayCommand]); [fPlayCommand]);
{$ENDIF} {$ENDIF}
end; end;
procedure Tplaysound.StopSound; procedure Tplaysound.StopSound;
begin begin
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
sndPlaySound(nil, SND_ASYNC or SND_NODEFAULT); sndPlaySound(nil, 0);
{$ELSE} {$ELSE}
if SoundPlayerSyncProcess <> nil then SoundPlayerSyncProcess.Terminate; if SoundPlayerSyncProcess <> nil then SoundPlayerSyncProcess.Terminate(1);
if SoundPlayerAsyncProcess <> nil then SoundPlayerAsyncProcess.Terminate; if SoundPlayerAsyncProcess <> nil then SoundPlayerAsyncProcess.Terminate(1);
{$ENDIF} {$ENDIF}
end; end;