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,40 +168,49 @@ 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
if fPlayStyle = psASync then L := TStringList.Create;
begin try
if SoundPlayerAsyncProcess = nil then L.Delimiter := ' ';
SoundPlayerAsyncProcess := Tasyncprocess.Create(nil); L.DelimitedText := fPlayCommand;
SoundPlayerAsyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename); if fPlayStyle = psASync then
SoundPlayerAsyncProcess.Executable := begin
FindDefaultExecutablePath(fPlayCommand); if SoundPlayerAsyncProcess = nil then
SoundPlayerAsyncProcess.Parameters.Clear; SoundPlayerAsyncProcess := Tasyncprocess.Create(nil);
SoundPlayerAsyncProcess.Parameters.Add(szSoundFilename); SoundPlayerAsyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename);
try SoundPlayerAsyncProcess.Executable := FindDefaultExecutablePath(L[0]);
SoundPlayerAsyncProcess.Execute; SoundPlayerAsyncProcess.Parameters.Clear;
except for i := 1 to L.Count-1 do
On E: Exception do SoundPlayerAsyncProcess.Parameters.Add(L[i]);
E.CreateFmt('Playstyle=paASync: ' + C_UnableToPlay + SoundPlayerAsyncProcess.Parameters.Add(szSoundFilename);
'%s Message:%s', [szSoundFilename, E.Message]); try
end; SoundPlayerAsyncProcess.Execute;
end except
else On E: Exception do
begin E.CreateFmt('Playstyle=paASync: ' + C_UnableToPlay +
if SoundPlayerSyncProcess = nil then '%s Message:%s', [szSoundFilename, E.Message]);
SoundPlayerSyncProcess := Tprocess.Create(nil); end;
SoundPlayerSyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename); end
SoundPlayerSyncProcess.Executable := else
FindDefaultExecutablePath(fPlayCommand); begin
SoundPlayersyncProcess.Parameters.Clear; if SoundPlayerSyncProcess = nil then
SoundPlayerSyncProcess.Parameters.Add(szSoundFilename); SoundPlayerSyncProcess := Tprocess.Create(nil);
try SoundPlayerSyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename);
SoundPlayerSyncProcess.Execute; SoundPlayerSyncProcess.Executable := FindDefaultExecutablePath(L[0]);
SoundPlayersyncProcess.WaitOnExit; SoundPlayersyncProcess.Parameters.Clear;
except for i:=1 to L.Count-1 do
On E: Exception do SoundPlayerSyncProcess.Parameters.Add(L[i]);
E.CreateFmt('Playstyle=paSync: ' + C_UnableToPlay + SoundPlayerSyncProcess.Parameters.Add(szSoundFilename);
'%s Message:%s', [szSoundFilename, E.Message]); try
SoundPlayerSyncProcess.Execute;
SoundPlayersyncProcess.WaitOnExit;
except
On E: Exception do
E.CreateFmt('Playstyle=paSync: ' + C_UnableToPlay +
'%s Message:%s', [szSoundFilename, E.Message]);
end;
end; end;
finally
L.Free;
end; end;
end end
else else
@ -209,13 +218,14 @@ begin
[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;