Recent

Author Topic: How to include sounds with tplaysound lib into exe???  (Read 2932 times)

lestroso

  • Full Member
  • ***
  • Posts: 134
    • FaSasoftware
How to include sounds with tplaysound lib into exe???
« on: June 29, 2019, 09:59:40 pm »
How to include sounds with tplaysound lib into exe??? i can put a path...but i don't want it in exe....

is it possible to include any resources in exe or app?? in a single file?? no folder??

i thank you a lot in advance....Lestroso :-[

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: How to include sounds with tplaysound lib into exe???
« Reply #1 on: June 29, 2019, 10:13:39 pm »
Go to the project's options (menu: "Project -> Project Options"), then to "Resources" and add the sound as a resource. It'll be embedded into the exe.

When you need, if you have no other option, you can save back the resource to a normal file and use it.

There is quite enough documentation on the use of resources both in the normal docs and the wiki. Good luck!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 13480
Re: How to include sounds with tplaysound lib into exe???
« Reply #2 on: June 29, 2019, 11:12:17 pm »
From all that I can see, the TPlaySound component does not have a stream interface. Therefore, you must extract the resource to a temporary file which can be passed to the component.

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: How to include sounds with tplaysound lib into exe???
« Reply #3 on: June 30, 2019, 01:33:28 am »
[...] Therefore, you must extract the resource to a temporary file which can be passed to the component.

Thining about it, rather than extract to a temp file I would simply extract it, on installation or first run, to the program's  or configuration folder and use it from there from then on.

Depending on the size of the files, extracting on each run may take significant time and make your program "slow to run", from the user's point of view.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to include sounds with tplaysound lib into exe???
« Reply #4 on: June 30, 2019, 02:29:51 am »
@lucamar

You are right if you want to play a 3-minute popsong. Or Beethovens 5th.

But if you only use a sound for the user interface, with the "modern" PCs you won't recognize it.

What I do is the following scheme:

for every sound exists s flag
with the  first use of the sound it is saved as file and the flag is set
the next times you know that a file exists

on finalization all used files are deleted

Winni
Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: How to include sounds with tplaysound lib into exe???
« Reply #5 on: June 30, 2019, 05:26:29 am »
You are right if you want to play a 3-minute popsong. Or Beethovens 5th.

I wouldn't want either, thanks :D

Yes, your scheme is a nice way to do it if they realy are short, small files (say up to 200KiB). But in that case, and unless there are hundreds of them, why not keep them around once "unpacked"? That way you do it just once.

By the way:
for every sound exists s flag
with the  first use of the sound it is saved as file and the flag is set
the next times you know that a file exists
why not just check with FileExists()?
(I'm serious; I'm curious about why you prefer using a flag)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

minesadorada

  • Sr. Member
  • ****
  • Posts: 453
  • Retired
Re: How to include sounds with tplaysound lib into exe???
« Reply #6 on: June 30, 2019, 07:02:44 am »
I wrote TPlaysound for a SkiRun game that I wanted to cross-compile to Linux.  Here is the code I used to extract the WAVfiles from the exe. Two files are extracted by name; the rest are assigned a number (the program later selects and plays them at random)
The files are extracted once only - when the app is first run.  Note - if the user deletes them they will be re-extracted on next run.

In Form_Create:
Code: Pascal  [Select][+][-]
  1.   // Make up physical files from embedded resource RCDATA
  2.   if (not FileExists('boing.wav')) or (not FileExists('cheering.wav')) then
  3.   begin
  4.     MakeSoundFiles('boing', 'boing.wav');
  5.     MakeSoundFiles('cheer', 'cheering.wav');
  6.   end;
  7.   for F := 1 to 15 do
  8.   begin
  9.     s := RandSounds[f - 1];
  10.     if (not FileExists(Format('0%d.wav', [f]))) then
  11.       MakeSoundFiles(Format('%s', [s]), Format('%d.wav', [f]));
  12.   end;
  13.  
(Note: RandSounds is an array containing the WAVfile names in the resource data)


..and the procedure that extracts the files:
Code: Pascal  [Select][+][-]
  1.  procedure Tmainform.MakeSoundFiles(wavename, wavefilename: string);
  2. // Extracts the embedded WAV files from the compiled EXE and then
  3. // saves them to the Application folder.
  4. // Only called at start-up if one or more of the WAV files are missing
  5. var
  6.   S: TResourceStream;
  7.   F: TFileStream;
  8. begin
  9.   // create a resource stream which points to our resource
  10.   S := TResourceStream.Create(HInstance, wavename, RT_RCDATA);
  11.   if (S = nil) or (S.Size = 0) then
  12.   begin
  13.     ShowMessage(Format('Cannot load resource named %s', [wavename]));
  14.     Exit;
  15.   end;
  16.   // Todo: handle case when stream is empty...
  17.   try
  18.     // create a file in the application directory
  19.     F := TFileStream.Create(ExtractFilePath(ParamStr(0)) + wavefilename, fmCreate);
  20.     try
  21.       F.CopyFrom(S, S.Size); // copy data from the resource stream to file stream
  22.     finally
  23.       F.Free; // destroy the file stream
  24.     end;
  25.   finally
  26.     S.Free; // destroy the resource stream
  27.   end;
  28.  
  29. end;          
  30.  
« Last Edit: June 30, 2019, 07:15:44 am by minesadorada »
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to include sounds with tplaysound lib into exe???
« Reply #7 on: June 30, 2019, 05:16:54 pm »
Quote
why not just check with FileExists()?
(I'm serious; I'm curious about why you prefer using a flag)

Just old bad habits from the IDE times, when filexists on windows took a notable amount of time.

Sorry, Winni
 https://forum.lazarus.freepascal.org/Smileys/ExcellentSmileys1/wacko.gif
 

jamie

  • Hero Member
  • *****
  • Posts: 7651
Re: How to include sounds with tplaysound lib into exe???
« Reply #8 on: June 30, 2019, 10:00:12 pm »
PlaySound API call in Windows can Play a WAV file directly from a resource.

PlaySound(SoundNameInTheResource, HInstance, SND_RESOURCE);

It can also play a Wave directly from memory,
PlaySound(AMemoryPointer,0, SND_MEMORY);

and so on...
The only true wisdom is knowing you know nothing

lestroso

  • Full Member
  • ***
  • Posts: 134
    • FaSasoftware
Re: How to include sounds with tplaysound lib into exe???
« Reply #9 on: July 02, 2019, 06:43:24 pm »
Dear friends,

i thank to everybody....but in truth i need a solution cross platform... win mac android linux html.....

i don't know why , the tplay sound don't work with mac os x... i have high sierra... 10.13.6........

Is it there a library true crossplatform? or a Tplay component library ...but very easy? because i'm a beginner ...i'm sorry about my problem...but i'm very happy to have you as friends......I thank you in advance....Best regards, lestroso

 

TinyPortal © 2005-2018