Sign in Join
Wiki copytemporary

Play Sound Multiplatform: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Play Sound Multiplatform

OldNewLine
1+## Summary
2+
3+- The object is to play a simple WAV sound both in Windows and Linux (Sync and ASync)
4+- There are lots of libraries to do this in a complicated way, but this code should suffice for simple use
5+
6+### Download Component
7+
8+Download from the lazarus CCR [here](https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/playsoundpackage/) (playsoundpackage)
9+
10+### Installation of component
11+
12+- Download all the 'playsoundpackage' package files from the Lazarus CCR repository and copy the whole folder structure to an empty folder of your lazarus components folder.
13+- Install the package (open it, Compile then Install it) and **TPlaysound** will appear on your 'LazControls' components tab
14+- Drop onto a form, set properties and you are good to go.
15+- PlayCommand property is populated automatically by testing which will work with your system.
16+- Opening the demo application will give you a good idea how to set the additional properties and use the component.
17+
18+### Version
19+
20+- As reported in the IDE. Initial version is 0.0.2
21+
22+### License
23+
24+- LGPLv2
25+
26+## Code
27+
28+- Here is the Uses clause in the Implementation section:
29+
30+``` pascal
31+..other units..
32+FileUtil{$IFDEF WINDOWS},mmsystem{$ELSE},asyncprocess,process{$ENDIF},StrUtils
33+..other units..
34+```
35+
36+- Declare a type:
37+
38+``` pascal
39+TPlayStyle = (psAsync,psSync);
40+```
41+
42+- Declare some variables:
43+
44+``` pascal
45+{$IFNDEF WINDOWS}
46+SoundPlayerAsyncProcess:Tasyncprocess;
47+SoundPlayerSyncProcess:Tprocess;
48+{$ENDIF}
49+fPathToSoundFile:String;
50+fPlayStyle:TPlayStyle;
51+```
52+
53+- And a worker function:
54+
55+``` pascal
56+procedure PlaySound(Const szSoundFilename:String);
57+```
58+
59+- And a couple of constants:
60+
61+``` pascal
62+CONST C_UnableToPlay = 'Unable to play ';
63+{$IFNDEF WINDOWS}
64+ // Defined in mmsystem
65+ SND_SYNC=0;
66+ SND_ASYNC=1;
67+ SND_NODEFAULT=2;
68+{$ENDIF}
69+```
70+
71+- Now you are good to go:
72+
73+``` pascal
74+fPlayStyle := psASync;
75+fPathToSoundFile:='mysound.wav';
76+If FileExistsUTF8(fPathToSoundFile) then PlaySound(fPathToSoundFile);
77+```
78+
79+- Here's the PlaySound procedure
80+
81+``` pascal
82+procedure PlaySound(const szSoundFilename: string);
83+var
84+ flags: word;
85+ szNonWindowsPlayCommand: string;
86+begin
87+ szNonWindowsPlayCommand := '';
88+{$IFDEF WINDOWS}
89+ if fPlayStyle = psASync then
90+ flags := SND_ASYNC or SND_NODEFAULT
91+ else
92+ flags := SND_SYNC or SND_NODEFAULT;
93+ try
94+ sndPlaySound(PChar(szSoundFilename), flags);
95+ except
96+ On E: Exception do
97+ E.CreateFmt(C_UnableToPlay +
98+ '%s Message:%s', [szSoundFilename, E.Message]);
99+ end;
100+{$ELSE}
101+ // How to play in Linux? Use generic Linux commands
102+ // Use asyncprocess to play sound as SND_ASYNC
103+ // Try play
104+ if (FindDefaultExecutablePath('play') <> '') then
105+ szNonWindowsPlayCommand := 'play -q';
106+ // Try aplay
107+ if (szNonWindowsPlayCommand = '') then
108+ if (FindDefaultExecutablePath('aplay') <> '') then
109+ szNonWindowsPlayCommand := 'aplay -q ';
110+ // Try paplay
111+ if (szNonWindowsPlayCommand = '') then
112+ if (FindDefaultExecutablePath('paplay') <> '') then
113+ szNonWindowsPlayCommand := 'paplay';
114+ // Try mplayer
115+ if (szNonWindowsPlayCommand = '') then
116+ if (FindDefaultExecutablePath('mplayer') <> '') then
117+ szNonWindowsPlayCommand := 'mplayer -really-quiet ';
118+ // Try CMus
119+ if (szNonWindowsPlayCommand = '') then
120+ if (FindDefaultExecutablePath('CMus') <> '') then
121+ szNonWindowsPlayCommand := 'CMus ';
122+ // Try pacat
123+ if (szNonWindowsPlayCommand = '') then
124+ if (FindDefaultExecutablePath('pacat') <> '') then
125+ szNonWindowsPlayCommand := 'pacat -p ';
126+ // Try ffplay
127+ if (szNonWindowsPlayCommand = '') then
128+ if (FindDefaultExecutablePath('ffplay') <> '') then
129+ szNonWindowsPlayCommand := 'ffplay -autoexit -nodisp -loglevel quiet';
130+ // Try cvlc
131+ if (szNonWindowsPlayCommand = '') then
132+ if (FindDefaultExecutablePath('cvlc') <> '') then
133+ szNonWindowsPlayCommand := 'cvlc -q --play-and-exit ';
134+ // Try canberra-gtk-play
135+ if (szNonWindowsPlayCommand = '') then
136+ if (FindDefaultExecutablePath('canberra-gtk-play') <> '') then
137+ szNonWindowsPlayCommand := 'canberra-gtk-play -c never -f ';
138+ // Try Macintosh command?
139+ if (szNonWindowsPlayCommand = '') then
140+ if (FindDefaultExecutablePath('afplay') <> '') then
141+ szNonWindowsPlayCommand := 'afplay';
142+ // Try mpg321
143+ if (szNonWindowsPlayCommand = '') then
144+ if (FindDefaultExecutablePath('mpg321') <> '') then
145+ szNonWindowsPlayCommand := 'mpg321 -q';
146+ // proceed if we managed to find a valid command
147+ if (szNonWindowsPlayCommand <> '') then
148+ begin
149+ if fPlayStyle = psASync then
150+ begin
151+ if SoundPlayerAsyncProcess = nil then
152+ SoundPlayerAsyncProcess := Tasyncprocess.Create(nil);
153+ SoundPlayerAsyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename);
154+ SoundPlayerAsyncProcess.Executable :=
155+ FindDefaultExecutablePath(Copy2Space(szNonWindowsPlayCommand));
156+ SoundPlayerAsyncProcess.Parameters.Clear;
157+ SoundPlayerAsyncProcess.Parameters.Add(szSoundFilename);
158+ try
159+ SoundPlayerAsyncProcess.Execute;
160+ except
161+ On E: Exception do
162+ E.CreateFmt('Playstyle=paASync: ' + C_UnableToPlay +
163+ '%s Message:%s', [szSoundFilename, E.Message]);
164+ end;
165+ end
166+ else
167+ begin
168+ if SoundPlayerSyncProcess = nil then
169+ SoundPlayerSyncProcess := Tprocess.Create(nil);
170+ SoundPlayerSyncProcess.CurrentDirectory := ExtractFileDir(szSoundFilename);
171+ SoundPlayerSyncProcess.Executable :=
172+ FindDefaultExecutablePath(Copy2Space(szNonWindowsPlayCommand));
173+ SoundPlayersyncProcess.Parameters.Clear;
174+ SoundPlayerSyncProcess.Parameters.Add(szSoundFilename);
175+ try
176+ SoundPlayerSyncProcess.Execute;
177+ SoundPlayersyncProcess.WaitOnExit;
178+ except
179+ On E: Exception do
180+ E.CreateFmt('Playstyle=paSync: ' + C_UnableToPlay +
181+ '%s Message:%s', [szSoundFilename, E.Message]);
182+ end;
183+ end;
184+ end
185+ else
186+ raise Exception.CreateFmt('The play command %s does not work on your system',
187+ [szNonWindowsPlayCommand]);
188+{$ENDIF}
189+end;
190+```
191+
192+- Destroy the process objects in your Destroy procedure:
193+
194+``` pascal
195+{$IFNDEF WINDOWS}
196+FreeAndNil(SoundPlayerSyncProcess);
197+FreeAndNil(SoundPlayerAsyncProcess);
198+{$ENDIF}
199+```
200+
201+---
202+*From the Free Pascal wiki, page "Play Sound Multiplatform", as it stood on 2024-03-12 after a change by Roland57, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
203+Category: Audio
204+Category: Components

Back to history