Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use wave file from project

I currently can only playback my background sound from having my wave file next to my compiled exe. But I actually want to have a single static executable with the 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.
like image 270
HitomiTenshi Avatar asked Nov 30 '22 14:11

HitomiTenshi


1 Answers

You can add the SND_MEMORY flag, and pass a TResourceStream.Memory pointer as the first parameter.

First, use XE2's Project->Resources and Images menu item to add a new resource. Give it the path and filename of your .wav file, a resource type of RC_DATA (it's not in the drop down list, but you can manually type it in), and a resource name you can use at runtime to refer to it. (In my example, I'm using C:\Microsoft Office\Office12\MEDIA\APPLAUSE.WAV, and giving it a resource name of 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;
like image 144
Ken White Avatar answered Dec 06 '22 22:12

Ken White