Recent

Author Topic: PlaySound doesn't play sound from resource  (Read 4947 times)

shyub

  • Full Member
  • ***
  • Posts: 142
Re: PlaySound doesn't play sound from resource
« Reply #15 on: May 25, 2024, 02:47:46 pm »
Hello!
The topic is old, I tried to repeat everything suggested, but I couldn’t reproduce the sound from the resource file. If possible, post a working example of the project.

wp

  • Hero Member
  • *****
  • Posts: 12069
Re: PlaySound doesn't play sound from resource
« Reply #16 on: May 25, 2024, 05:02:01 pm »
See attached

shyub

  • Full Member
  • ***
  • Posts: 142
Re: PlaySound doesn't play sound from resource
« Reply #17 on: May 25, 2024, 06:16:15 pm »
Thank you!
I get the *.LRS file using the "lazres.exe" utility with the command "lazres sound.lrs ALARM02.wav".
How did you get the "sound.res" file?

TRon

  • Hero Member
  • *****
  • Posts: 2790
Re: PlaySound doesn't play sound from resource
« Reply #18 on: May 25, 2024, 06:25:57 pm »
I get the *.LRS file using the "lazres.exe" utility with the command "lazres sound.lrs ALARM02.wav".
Please do not use that (anymore). Instead see here.

shyub

  • Full Member
  • ***
  • Posts: 142
Re: PlaySound doesn't play sound from resource
« Reply #19 on: May 25, 2024, 06:34:06 pm »
I didn't formulate the question quite correctly. I have a file "Bell.wav", how can I put it in the resources instead of "ALARM02"?

wp

  • Hero Member
  • *****
  • Posts: 12069
Re: PlaySound doesn't play sound from resource
« Reply #20 on: May 25, 2024, 07:03:31 pm »
The easiest way to add a resource to an application - without any other tools - is to go to "Project" > "Project options" > "Project options" > "Resources"; click "Add", switch the file type filter to "All files (*.*)"; select the file to be added > "Open". The file is now listed in the "Resources" windows. Note the name in the "Resource" column - this is the name which you must use to open the resource in the application. To the left of it, you see "RCDATA" in the "Type" column; this is the type of the resource which you usually must specifiy when you load the resource (you must preceed the type name with an "RT_").

If you follow this, your "Bell.wav" will be in the resources as BELL of type RCDATA. In order to open it in your application as a resource stream you must write
Code: Pascal  [Select][+][-]
  1.   resStream := TResourceStream.Create(HINSTANCE, 'BELL', RT_RCDATA);

A little disadvantage of this method is that the files added to the resources must be available whenever the application is compiled. The "lazres" utility, on the other hand, allows you to create resource files independent of compilation.

shyub

  • Full Member
  • ***
  • Posts: 142
Re: PlaySound doesn't play sound from resource
« Reply #21 on: May 25, 2024, 07:51:44 pm »
wp, thank you very much!

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: PlaySound doesn't play sound from resource
« Reply #22 on: May 25, 2024, 07:55:56 pm »
The easiest way to add a resource to an application - without any other tools - is to go to "Project" > "Project options" > "Project options" > "Resources"; click "Add", switch the file type filter to "All files (*.*)"; select the file to be added > "Open". The file is now listed in the "Resources" windows. Note the name in the "Resource" column - this is the name which you must use to open the resource in the application. To the left of it, you see "RCDATA" in the "Type" column; this is the type of the resource which you usually must specifiy when you load the resource (you must preceed the type name with an "RT_").

If you follow this, your "Bell.wav" will be in the resources as BELL of type RCDATA. In order to open it in your application as a resource stream you must write
Code: Pascal  [Select][+][-]
  1.   resStream := TResourceStream.Create(HINSTANCE, 'BELL', RT_RCDATA);

A little disadvantage of this method is that the files added to the resources must be available whenever the application is compiled. The "lazres" utility, on the other hand, allows you to create resource files independent of compilation.
or, also within this tool called Lazarus, create a new file, add
Code: Pascal  [Select][+][-]
  1. LANGUAGE 0,0
  2. #define RCDATA 10
  3. Bell RCDATA "Bell.wav"
save file as "sounds.rc" in project folder.
copy bell.wav into project folder.
inside code use "{$R 'sounds.rc'}" near under "implementation"
in theory its the same as adding by project options, since its just text its for me more comfortable.
requires a full fpc or lazarus installation, wherever the binary resource compiler is deployed with.
« Last Edit: May 25, 2024, 08:01:09 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

TRon

  • Hero Member
  • *****
  • Posts: 2790
Re: PlaySound doesn't play sound from resource
« Reply #23 on: May 25, 2024, 11:43:03 pm »
The "lazres" utility, on the other hand, allows you to create resource files independent of compilation.
It is advisable not to use it anymore. See KodeZwerg's reply.

requires a full fpc or lazarus installation, wherever the binary resource compiler is deployed with.
And in case it is unable to locate it fpc tries to fall back to winres (which can be optionally configured and installed manually). The fpcres utility has to go through 2 releases to get fully integrated (into the build process).

 

TinyPortal © 2005-2018