Changeset 178 for branches/highdpi/Packages/CevoComponents/Sound.pas
- Timestamp:
- Jun 23, 2019, 3:15:29 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/highdpi/Packages/CevoComponents/Sound.pas
r174 r178 4 4 5 5 uses 6 Messages, SysUtils, Classes, Graphics, Controls, Forms, fgl, FileUtil, 7 StringTables, Directories 8 {$IFDEF WINDOWS}, MMSystem, Windows{$ENDIF} 9 {$IFDEF LINUX}, Process, AsyncProcess{$ENDIF}; 6 Messages, SysUtils, Classes, Graphics, Controls, Forms, fgl 7 {$IFDEF WINDOWS}, MMSystem, Windows{$ENDIF}; 10 8 11 9 type 12 TPlayStyle = (psAsync, psSync);13 14 10 TSoundPlayer = class(TForm) 15 11 private … … 19 15 end; 20 16 21 { TSound } 17 function PrepareSound(FileName: string): integer; 18 procedure PlaySound(FileName: string); 22 19 20 implementation 21 22 {$R *.lfm} 23 24 type 23 25 TSound = class 24 private25 PlayCommand: string;26 {$IFDEF LINUX}27 SoundPlayerAsyncProcess: TAsyncProcess;28 SoundPlayerSyncProcess: TProcess;29 {$ENDIF}30 function GetNonWindowsPlayCommand: string;31 26 public 32 FDeviceID: Word;27 FDeviceID: word; 33 28 FFileName: string; 34 PlayStyle: TPlayStyle;35 29 constructor Create(const FileName: string); 36 30 destructor Destroy; override; … … 40 34 end; 41 35 42 function PrepareSound(FileName: string): Integer;43 procedure PlaySound(FileName: string);44 function Play(Item: string; Index: Integer = -1): Boolean;45 procedure PreparePlay(Item: string; Index: Integer = -1);46 47 const48 // sound modes49 smOff = 0;50 smOn = 1;51 smOnAlt = 2;52 53 var54 Sounds: TStringTable;55 SoundMode: Integer;56 SoundPlayer: TSoundPlayer;57 SoundList: TFPGObjectList<TSound>;58 PlayingSound: TSound;59 60 61 implementation62 63 {$R *.lfm}64 65 resourcestring66 SUnableToPlay = 'PlayStyle=%s: Unable to play %s Message:%s';67 SPlayCommandNotWork = 'The play command %s does not work on your system';68 36 69 37 constructor TSound.Create(const FileName: string); … … 73 41 {$ENDIF} 74 42 begin 75 PlayStyle := psAsync;76 FFileName := FileName;77 43 {$IFDEF WINDOWS} 78 44 FDeviceID := 0; 79 if FileExists(FFileName) then begin 45 FFileName := FileName; 46 if FileExists(FFileName) then 47 begin 80 48 OpenParm.dwCallback := 0; 81 49 OpenParm.lpstrDeviceType := 'WaveAudio'; … … 85 53 FDeviceID := OpenParm.wDeviceID; 86 54 end 87 {$ENDIF}88 {$IFDEF LINUX}89 PlayCommand := GetNonWindowsPlayCommand;90 FDeviceID := 1;91 55 {$ENDIF} 92 56 end; … … 98 62 mciSendCommand(FDeviceID, MCI_CLOSE, MCI_WAIT, 0); 99 63 {$ENDIF} 100 {$IFDEF LINUX}101 FreeAndNil(SoundPlayerSyncProcess);102 FreeAndNil(SoundPlayerAsyncProcess);103 {$ENDIF}104 64 inherited Destroy; 105 65 end; 106 107 function TSound.GetNonWindowsPlayCommand: string;108 begin109 Result := '';110 // Try play111 if (FindDefaultExecutablePath('play') <> '') then112 Result := 'play';113 // Try aplay114 if (result = '') then115 if (FindDefaultExecutablePath('aplay') <> '') then116 Result := 'aplay -q';117 // Try paplay118 if (Result = '') then119 if (FindDefaultExecutablePath('paplay') <> '') then120 Result := 'paplay';121 // Try mplayer122 if (Result = '') then123 if (FindDefaultExecutablePath('mplayer') <> '') then124 Result := 'mplayer -really-quiet';125 // Try CMus126 if (Result = '') then127 if (FindDefaultExecutablePath('CMus') <> '') then128 Result := 'CMus';129 // Try pacat130 if (Result = '') then131 if (FindDefaultExecutablePath('pacat') <> '') then132 Result := 'pacat -p';133 // Try ffplay134 if (Result = '') then135 if (FindDefaultExecutablePath('ffplay') <> '') then136 result := 'ffplay -autoexit -nodisp';137 // Try cvlc138 if (Result = '') then139 if (FindDefaultExecutablePath('cvlc') <> '') then140 result := 'cvlc -q --play-and-exit';141 // Try canberra-gtk-play142 if (Result = '') then143 if (FindDefaultExecutablePath('canberra-gtk-play') <> '') then144 Result := 'canberra-gtk-play -c never -f';145 // Try Macintosh command?146 if (Result = '') then147 if (FindDefaultExecutablePath('afplay') <> '') then148 Result := 'afplay';149 end;150 151 66 152 67 procedure TSound.Play(HWND: DWORD); … … 154 69 var 155 70 PlayParm: TMCI_Play_Parms; 156 {$ENDIF}157 {$IFDEF LINUX}158 var159 L: TStringList;160 I: Integer;161 71 {$ENDIF} 162 72 begin … … 168 78 end 169 79 {$ENDIF} 170 {$IFDEF LINUX}171 // How to play in Linux? Use generic Linux commands172 // Use asyncprocess to play sound as SND_ASYNC173 // proceed if we managed to find a valid command174 if PlayCommand <> '' then begin175 L := TStringList.Create;176 try177 L.Delimiter := ' ';178 L.DelimitedText := PlayCommand;179 if PlayStyle = psASync then begin180 if SoundPlayerAsyncProcess = nil then181 SoundPlayerAsyncProcess := TAsyncProcess.Create(nil);182 SoundPlayerAsyncProcess.CurrentDirectory := ExtractFileDir(FFilename);183 SoundPlayerAsyncProcess.Executable := FindDefaultExecutablePath(L[0]);184 SoundPlayerAsyncProcess.Parameters.Clear;185 for I := 1 to L.Count - 1 do186 SoundPlayerAsyncProcess.Parameters.Add(L[I]);187 SoundPlayerAsyncProcess.Parameters.Add(FFilename);188 try189 SoundPlayerAsyncProcess.Execute;190 except191 On E: Exception do192 E.CreateFmt(SUnableToPlay, ['paASync', FFilename, E.Message]);193 end;194 PlayingSound := nil;195 end else begin196 if SoundPlayerSyncProcess = nil then197 SoundPlayerSyncProcess := TProcess.Create(nil);198 SoundPlayerSyncProcess.CurrentDirectory := ExtractFileDir(FFilename);199 SoundPlayerSyncProcess.Executable := FindDefaultExecutablePath(L[0]);200 SoundPlayersyncProcess.Parameters.Clear;201 for I := 1 to L.Count - 1 do202 SoundPlayerSyncProcess.Parameters.Add(L[I]);203 SoundPlayerSyncProcess.Parameters.Add(FFilename);204 try205 SoundPlayerSyncProcess.Execute;206 SoundPlayersyncProcess.WaitOnExit;207 except208 On E: Exception do209 E.CreateFmt(SUnableToPlay, ['paSync', FFilename, E.Message]);210 end;211 PlayingSound := nil;212 end;213 finally214 L.Free;215 end;216 end217 else218 raise Exception.CreateFmt(SPlayCommandNotWork, [PlayCommand]);219 {$ENDIF}220 80 end; 221 81 … … 224 84 {$IFDEF WINDOWS} 225 85 mciSendCommand(FDeviceID, MCI_STOP, 0, 0); 226 {$ENDIF}227 {$IFDEF LINUX}228 if SoundPlayerSyncProcess <> nil then SoundPlayerSyncProcess.Terminate(1);229 if SoundPlayerAsyncProcess <> nil then SoundPlayerAsyncProcess.Terminate(1);230 86 {$ENDIF} 231 87 end; … … 237 93 {$ENDIF} 238 94 end; 95 96 97 var 98 SoundPlayer: TSoundPlayer; 99 SoundList: TFPGObjectList<TSound>; 100 PlayingSound: TSound; 239 101 240 102 {$IFDEF WINDOWS} … … 249 111 {$ENDIF} 250 112 251 function PrepareSound(FileName: string): Integer;113 function PrepareSound(FileName: string): integer; 252 114 begin 253 115 Result := 0; 254 while ( Result < SoundList.Count) and (SoundList[result].FFileName <> FileName) do255 Inc(Result);256 if Result = SoundList.Count then begin257 // First time this sound is played116 while (result < SoundList.Count) and (SoundList[result].FFileName <> FileName) do 117 inc(result); 118 if result = SoundList.Count then begin 119 // first time this sound is played 258 120 SoundList.Add(TSound.Create(FileName)); 259 121 Result := SoundList.Count - 1; … … 263 125 procedure PlaySound(FileName: string); 264 126 begin 265 if PlayingSound <> nil then Exit; 127 if PlayingSound <> nil then 128 exit; 266 129 if SoundPlayer = nil then 267 130 Application.CreateForm(TSoundPlayer, SoundPlayer); … … 271 134 else 272 135 PlayingSound.Play(SoundPlayer.Handle); 273 end;274 275 function Play(Item: string; Index: Integer = -1): Boolean;276 {$IFNDEF DEBUG}277 var278 WavFileName: string;279 {$ENDIF}280 begin281 Result := False;282 {$IFNDEF DEBUG}283 if (Sounds = nil) or (SoundMode = smOff) or (Item = '') then284 begin285 Result := True;286 Exit;287 end;288 WavFileName := Sounds.Lookup(Item, Index);289 Assert(WavFileName[1] <> '[');290 Result := (WavFileName <> '') and (WavFileName[1] <> '[') and (WavFileName <> '*');291 if Result then292 // SndPlaySound(pchar(HomeDir+'Sounds' +DirectorySeparator+WavFileName+'.wav'),SND_ASYNC)293 PlaySound(HomeDir + 'Sounds' + DirectorySeparator + WavFileName);294 {$ENDIF}295 end;296 297 procedure PreparePlay(Item: string; Index: Integer = -1);298 {$IFNDEF DEBUG}299 var300 WavFileName: string;301 {$ENDIF}302 begin303 {$IFNDEF DEBUG}304 if (Sounds = nil) or (SoundMode = smOff) or (Item = '') then305 Exit;306 WavFileName := Sounds.Lookup(Item, Index);307 Assert(WavFileName[1] <> '[');308 if (WavFileName <> '') and (WavFileName[1] <> '[') and (WavFileName <> '*') then309 PrepareSound(HomeDir + 'Sounds' + DirectorySeparator + WavFileName);310 {$ENDIF}311 136 end; 312 137 … … 325 150 end; 326 151 FreeAndNil(SoundList); 327 if Sounds <> nil then328 FreeAndNil(Sounds);329 152 end; 330 153
Note:
See TracChangeset
for help on using the changeset viewer.