Use wave file from project

Currently, I can only play my background sound from my wave file next to my compiled exe. But I really want to have one static executable with a wave file inside. Is this possible in Delphi XE2?

This is my code:

SndPlaySound('.\Raw.wav', SND_ASYNC or SND_LOOP);
#This will play the Raw.wav that is next to my program.
+3
source share
4 answers

If you use PlaySound()instead sndPlaySound(), you can use the flag SND_RESOURCEto play an audio signal directly from its resource without having to first load it into memory.

+5
source

You can add the SND_MEMORY flag and pass the pointer TResourceStream.Memoryas the first parameter.

XE2 Project->Resources and Images, . .wav, RC_DATA ( , ), , , , ( C:\Microsoft Office\Office12\MEDIA\APPLAUSE.WAV APPLAUSE.)

procedure TForm2.Button1Click(Sender: TObject);
var
  Res: TResourceStream;
begin
  Res := TResourceStream.Create(HInstance, 'APPLAUSE', 'RC_DATA');
  try
    Res.Position := 0;
    SndPlaySound(Res.Memory, SND_MEMORY or SND_ASYNC or SND_LOOP);
  finally
    Res.Free;
  end;
end;
+9

Just tested and works on mine:

var
  hFind, hRes: THandle;
  Song       : PChar;
begin
  hFind := FindResource(HInstance, 'BATTERY', 'WAV');
  if (hFind <> 0) then
  begin
    hRes := LoadResource(HInstance, hFind);
    if (hRes <> 0) then
    begin
      Song := LockResource(hRes);
      if Assigned(Song) then
      begin
        SndPlaySound(Song, snd_ASync or snd_Memory);
      end;
      UnlockResource(hRes);
    end;
    FreeResource(hFind);
  end;

enter image description here

+1
source

enter "WAVE" as the resource type when importing the wav file into the Resource Editor (Delphi 10, Project, Resources and Images) and just use

   PlaySound(resourceIndentifierName, 0, SND_RESOURCE or SND_ASYNC);

PS uppercase is no longer required

0
source

All Articles