Recent

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

karloromic

  • New Member
  • *
  • Posts: 32
PlaySound doesn't play sound from resource
« on: September 25, 2019, 12:36:16 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, LResources, MMSystem;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. begin
  34.   PlaySound('ONLINE2', HInstance, SND_RESOURCE);
  35. end;
  36.  
  37. end.
  38.  
  39.  

Example project:
https://ufile.io/xw3w18ai
« Last Edit: September 25, 2019, 12:41:33 pm by karloromic »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 775
    • Lebeau Software
Re: PlaySound doesn't play sound from resource
« Reply #1 on: September 25, 2019, 09:58:08 pm »
Code: Pascal  [Select][+][-]
  1. PlaySound('ONLINE2', HInstance, SND_RESOURCE);

In your project1.lpi file, your "ONLINE2" resource is set to a resource type of "RCDATA":

Code: [Select]
<Resource_0 FileName="audio\online2.wav" Type="RCDATA" ResourceName="ONLINE2"/>
It needs to be set to a resource type of "WAVE" instead, or else PlaySound() will not be able to find it at runtime.

Otherwise, you can manually obtain a memory pointer to the resource data using FindResource()/LoadResource()/LockResource(), and then use SND_MEMORY instead of SND_RESOURCE to play it.
« Last Edit: September 25, 2019, 10:02:05 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

karloromic

  • New Member
  • *
  • Posts: 32
Re: PlaySound doesn't play sound from resource
« Reply #2 on: September 25, 2019, 10:21:30 pm »
Setting resource type "WAVE" instead of "RCDATA" doesnt work as it is automatically corrected on compilation..

Otherwise, you can manually obtain a memory pointer to the resource data using FindResource()/LoadResource()/LockResource(), and then use SND_MEMORY instead of SND_RESOURCE to play it.

Can you give me an simple example of that? Thank you

winni

  • Hero Member
  • *****
  • Posts: 1359
Re: PlaySound doesn't play sound from resource
« Reply #3 on: September 25, 2019, 10:38:51 pm »
Allways trouble with the Windows resource files. Neverending story.

Use the Lazarus resource files. You have to convert your WAV-file (or any other file) to an .lrs file:

lazres sound.lrs ONLINE2.WAV

And then copy the sound.lrs to your source file directory.

And in your unit1 you have to write

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. {$I sound.lrs }

One problem less.

Winni

karloromic

  • New Member
  • *
  • Posts: 32
Re: PlaySound doesn't play sound from resource
« Reply #4 on: September 25, 2019, 11:01:12 pm »
Allways trouble with the Windows resource files. Neverending story.

Use the Lazarus resource files. You have to convert your WAV-file (or any other file) to an .lrs file:

lazres sound.lrs ONLINE2.WAV

And then copy the sound.lrs to your source file directory.

And in your unit1 you have to write

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. {$I sound.lrs }

One problem less.

Winni

Wow, that solved it! Thanks!
 
And also syntax correction:
Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. {$I sound.lrs }

should be used on initialization:

Code: Pascal  [Select][+][-]
  1. initialization
  2. {$I sound.lrs}  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 775
    • Lebeau Software
Re: PlaySound doesn't play sound from resource
« Reply #5 on: September 26, 2019, 02:45:47 am »
Otherwise, you can manually obtain a memory pointer to the resource data using FindResource()/LoadResource()/LockResource(), and then use SND_MEMORY instead of SND_RESOURCE to play it.

Can you give me an simple example of that?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   hRes: HRSRC;
  4.   hGlob: HGLOBAL;
  5.   pMem: Pointer;
  6. begin
  7.   hRes := FindResource(HInstance, 'ONLINE2', RT_RCDATA{MAKEINTRESOURCE(10)});
  8.   if hRes = 0 then RaiseLastOSError;
  9.  
  10.   hGlob := LoadResource(HInstance, hRes);
  11.   if hGlob = 0 then RaiseLastOSError;
  12.  
  13.   pMem := LockResource(hGlob);
  14.   if pMem = nil then RaiseLastOSError;
  15.  
  16.   PlaySound(PChar(pMem), 0, SND_MEMORY);
  17. end;
  18.  
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018