The attached demo program reads two audio files from the program's resources and plays them using the cross-platform TPlaySound component.
Step 1: Creation of the resource fileI usually write a small batch file (I am on Windows) which calls the tool "lazres" to add the resource items. Its sources are found in the folder "tools" of the Lazarus installation. Compile it and copy the exe somewhere so that it is easily found. The first parameter is the name of the resource file to be created; the following parameters are the names of the files to be added to the resource (in case of a long list of file names, the file names can be put into a separate text file which is mentioned here with a preceding @). A typical syntax is
lazres resource.res sound1.wav sound2.wav
or
lazres resource.res @filelist.txt
with "filelist.txt" containing
sound1.wav
sound2.wav
This creates a resource file named "resource.res" containing two items "sound1" and "sound2" (the extension is dropped for the resource item name). These items have resource type RT_RCDATA
Step 2: Extract the resourceIn the program code link the resource file by adding a line {$R resource.res} containing the resource file name. Specify the (relative) path if the resource file is not in the project folder directly.
In order to extract a resource item, e.g. "sound1" create a TResourceStream and specify the name in the call:
var
stream: TResourceStream;
...
stream := TResourceStream.Create(HINSTANCE, 'sound1', RT_RCDATA);
'sound1' is the name of the resource to be extracted (see step 1), RT_RCDATA is the type of the resource.
Step 3: Play the soundUnfortunately, the PlaySound component has no direct stream interface. Therefore the stream must be written to a temporary file
var
tmpFileName: String;
...
tmpFileName := tmpFileName := AppendPathDelim(GetTempDir) + 'sound1.tmp';
stream.SaveToFile(tmpFileName);
In case of the TPlaySound component, the temporary filename must be passed to the property "SoundFile"; in order the play the sound, the method "Execute" must be called.
My demo contains a modified component unit which is usable without any further installation.
uses
uPlaySound;
var
player: TPlaySound;
...
player := TPlaySound.Create(nil);
try
player.SoundFile := tmpFileName;
player.Execute;
finally
player.Free;
end;
Step 4: Clean upWhen the program ends delete the temporary files (unless you want to keep them).
P.S.
Unfortunately, the resource file is too large for the forum, therefore I am posting only the program sources. Please send me a PM with your e-mail address and I'll return the full project with resource file.