In Linux, to play a WAV file your Lazarus/FPC program must "talk to" the sound server (the audio mixer). There may be different sound servers installed depending on the distribution and desktop type (e.g., Gnome vs. KDE).
On Ubuntu 8.04 LTS (Hardy Heron), where the default desktop is Gnome, the sound server is PulseAudio. The pulseaudio-utils package includes the paplay utility that can play WAV files. Here is an example:
// for systems using the PulseAudio sound server
// such as Linux Ubuntu Hardy Heron
uses
[...] FileUtil, Process;
function PlaySoundLnx(fileName: String): Boolean;
const
playerCmd = 'paplay'; // pulseaudio client
var
AProcess: TProcess;
begin
AProcess := TProcess.Create(nil);
with Aprocess do begin
CommandLine := FindDefaultExecutablePath(playerCmd) +
' ' + filename;
//Options := Options + [poWaitOnExit];
try
try
Execute;
except
on E: Exception do
ShowMessage(E.ClassName +
' error raised, with message : ' + E.Message);
end;
finally
Free;
end;
end;
end;
[...]
procedure PlayMyWAV;
begin
// get application directory;
// wav file is in the adudio subdirectory
AppDir := ExtractFilePath(Application.ExeName);
// play WAV
PlaySoundLnx(AppDir + 'audio/my.wav');
end;
The following simpler code may also work in PlaySoundLnx (instead of using TProcess) but I have not tried it:
SysUtils.ExecuteProcess(FindDefaultExecutablePath('paplay'),
AppDir + 'audio/my.wav');
On OpenSUSE and SLED (Gnome desktop) the sound server is esd and there is another posting in this forum that shows how to access the esd library directly from Laz/FPC.
On KDE-2/3-based desktops the sound server is usually aRts. Output from any sound-playing utility can be redirected to the aRts daemon (artsd) using the artsdsm utilty. The following should work:
artsdsp -m <player> my.wav
or
artsdsp -q <player> my.wav
where <player> can be one of: play (sox); aplay (alsa-utils); mplayer; mpeg123; etc.
The KDE-4 desktop uses Phonon.
You may also wish to read about how "PulseAudio Tames the Linux Audio Zoo", Part
1 and
2.