Recent

Author Topic: Play Embedded Sound File  (Read 5348 times)

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Play Embedded Sound File
« on: September 02, 2020, 09:43:11 am »
I am working my way through various forum topics trying to find a simple example of playing an embedded .wav file.

Project Options Resources does not have a wave type.

I've found how to create a resource file using lazres, and it seems to create it OK.

Now what .... ?

One topic mention include {$I resourcefile} but that resulted in a page of garbage. I changed it to $R, but still cannot compile.

Please give me a simple example to copy and edit.


Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: Play Embedded Sound File
« Reply #1 on: September 03, 2020, 06:03:38 am »
To include a WAV file in a resource, see this post.

Note that the Windows PlaySound API can Play a WAV file directly from a resource.
PlaySound(SoundNameInTheResource, HInstance, SND_RESOURCE);

Hint: many (most?) questions have already been asked and answered, so a forum search is often a good idea before posting.
« Last Edit: September 05, 2020, 02:25:25 am by trev »

alaa123456789

  • Sr. Member
  • ****
  • Posts: 261
  • Try your Best to learn & help others
    • youtube:
Re: Play Embedded Sound File
« Reply #2 on: September 03, 2020, 05:40:11 pm »

kupferstecher

  • Hero Member
  • *****
  • Posts: 618
Re: Play Embedded Sound File
« Reply #3 on: September 03, 2020, 06:23:10 pm »
One topic mention include {$I resourcefile} but that resulted in a page of garbage. I changed it to $R, but still cannot compile.

With $I you include the resource into a unit, you also have to add LResources to the uses. With this it compiles, loading the resource is an other topic. You can find details in the wiki, search for LazarusResources.
Code: Pascal  [Select][+][-]
  1. IMPLEMENTATION
  2. uses
  3.   LResources
  4.  
  5. //Procedures etc ...
  6.  
  7. INITIALIZATION
  8.   {$I someresource.lrs}
  9.  
  10. end.

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Play Embedded Sound File
« Reply #4 on: September 03, 2020, 09:03:34 pm »
Hint: many (most?) questions have already been asked and answered, so a forum search is often a good idea before posting.
Sure, and even so many useless answers have been given, same as in this thread (yet again).

Project options, add resource, and load it using resourcestream.
Today is tomorrow's yesterday.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Play Embedded Sound File
« Reply #5 on: September 08, 2020, 03:46:05 am »
To include a WAV file in a resource, see this post.

Note that the Windows PlaySound API can Play a WAV file directly from a resource.
PlaySound(SoundNameInTheResource, HInstance, SND_RESOURCE);

Hint: many (most?) questions have already been asked and answered, so a forum search is often a good idea before posting.

Hi Trev,

Thanks for your reply.

The old thread you quoted does not indicate what unit to use. The error is in

 S := TResourceStream.Create(HInstance, wavename, RT_RCDATA);

Note the sounds.rc file is in place and properly included

{$IFDEF WINDOWS}
{$r sounds.rc}// user defined resource file
{$ENDIF}

I then deleted Tmainform.MakeSoundFiles and used the playsound suggestion.

It now compiles OK, but no sound.

procedure TForm1.Button1Click(Sender: TObject);
begin
  PlaySound('Foghorn1', HInstance, SND_RESOURCE);
end;

The object is not to detach any files, but have the sound file play from within the .exe.

This is why I am struggling with old threads. It appears to be a follow on from a previews thread where the appropriate unit was declared.

Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: Play Embedded Sound File
« Reply #6 on: September 08, 2020, 08:26:14 am »
Delete your .rc file inclusion, include your wav file(s) from the Lazarus menu Project > Project Options > Project Options > Resources, Add file(s).

This code then works:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Stream: TResourceStream;
  4. begin
  5.   Stream := TResourceStream.Create(HInstance, 'BOOM', RT_RCDATA);
  6.   try
  7.     Stream.Position := 0;
  8.     SndPlaySound(Stream.Memory, SND_MEMORY);
  9.   finally
  10.     Stream.Free;
  11.   end;
  12. end;

Project attached [compatible version attached]
« Last Edit: September 08, 2020, 12:54:56 pm by trev »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Play Embedded Sound File
« Reply #7 on: September 08, 2020, 09:18:45 am »
hello,
trev i can't open your project on windows 10.
for windows  code working for me  :
Code: Pascal  [Select][+][-]
  1. implementation
  2.  uses LResources,windows, MMSystem;
  3. {$R *.lfm}
  4. { TForm1 }
  5. procedure TForm1.Button1Click(Sender: TObject);
  6. var
  7.   Stream: TResourceStream;
  8. begin
  9.   Stream := TResourceStream.Create(HInstance, 'BOOM', RT_RCDATA{MAKEINTRESOURCE(10)});
  10.   try
  11.     Stream.Position := 0;
  12.     SndPlaySound(Stream.Memory, SND_MEMORY);
  13.   finally
  14.     Stream.Free;
  15.   end;
  16. end;  

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: Play Embedded Sound File
« Reply #8 on: September 08, 2020, 12:58:03 pm »
trev i can't open your project on windows 10.

Oops - I forgot to tick Project Option > Miscellaneous [ ] Maximise compatibility of project files (using Lazarus trunk). Fixed.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Play Embedded Sound File
« Reply #9 on: September 09, 2020, 06:40:50 am »
Delete your .rc file inclusion, include your wav file(s) from the Lazarus menu Project > Project Options > Project Options > Resources, Add file(s).


Trev,

I didn't even get to your attachment (yet). Your code and actions worked first go. I'll still have a look at your project and all the other replies though.

Thank you.
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

 

TinyPortal © 2005-2018