Mega Code Archive

 
Categories / Delphi / Multimedia
 

Embed sound into your EXE and play it

Title: Embed sound into your EXE and play it Question: You want sound in your app, but you don't want to distribute the WAVe files. Maybe the EXE has to be eMailed, maybe you don't want users to re-use your sound files - either way, it would be nice to compile them into the EXE. Here's how to do it. Answer: Step 1) Make an SOUNDS.RC file Use NotePad - it should look like this: #define WAVE WAVEFILE SOUND1 WAVE "anysound.wav" SOUND2 WAVE "anthersound.wav" SOUND3 WAVE "hello.wav" Step 2) Compile it into a RES file Use the program that comes with Delphi - BRCC32.EXE. Use a command line that looks like: BRCC32.EXE -foSOUND32.RES SOUNDS.RC You should end up with a file called 'sound32.res' Step 3) Put it in your program In the DPR file, put it under the {$R *.RES} line like this: {$R SOUND32.RES} Step 4) Put this code in to play an embedded sound USES MMSYSTEM Procedure PlayResSound(RESName:String;uFlags:Integer); var hResInfo,hRes:Thandle; lpGlob:Pchar; Begin hResInfo:=FindResource(HInstance,PChar(RESName),MAKEINTRESOURCE('WAVEFILE')); if hResInfo = 0 then begin messagebox(0,'Could not find this Resource',PChar(RESName),16); exit; end; hRes:=LoadResource(HInstance,hResinfo); if hRes = 0 then begin messagebox(0,'Could not load this Resource',PChar(RESName),16); exit; end; lpGlob:=LockResource(hRes); if lpGlob=Nil then begin messagebox(0,'Resource bad.',PChar(RESName),16); exit; end; uFlags:=snd_Memory or uFlags; SndPlaySound(lpGlob,uFlags); UnlockResource(hRes); FreeResource(hRes); End; Step 5) Call the routine, using the names you gave the sound files in Step (1) PlayResSound('SOUND1',SND_ASYNC) Flags are: SND_ASYNC = Start playing, and don't wait to return SND_SYNC = Start playing, and wait for the sound to finish SND_LOOP = Keep looping the sound until another sound is played