This is helpful if you do not want to have a wave file in the directory
of the program you distribute. One problem with inserting a wave file into
your .exe is that it makes your programs .exe larger, so it is a good idea
if your program uses small wave files or maybe one or two large wave files.
The Delphi example below will work on Delphi versions above Delphi
2.
First open an editor like Notepad.exe, for this example we will use
a wave file called Laser.wav
and this is the wave file we will insert into our programs .exe
Type these words into the text file:
TheLaser Wave "Laser.wav"
Then save the text file as:
mywave.rc
We will use BRCC32.exe that comes with Delphi, it should be in Delphi's
Bin directory to compile the file.
In order to use BRCC32.exe you have to use a DOS window, using DOS
is not hard at all.
Whenever you use DOS all you have to do, to get back to Windows (Your
desktop) is type the word exit, then press the Enter key.
You should have your wave files and your .rc file in the same folder/directory.
So we will type this sentance in the DOS window:
(We are in the
c:\Delphi 3\sounds
directory)
c:\Delphi 3\sounds>BRCC32 MYWAVE.RC
If this does not work or you get an error, you may not have Delphi in
your computers path.
A way around this is for you to copy the BRCC32.exe and RW32CORE.DLL
into your folder/directory that you are using, then try again.
(The above files, BRCC32.exe and RW32CORE.DLL, may be different, for
different versions of Delphi)
Use Explorer or another File Manager and have a look in your directory, you should find that you have an extra file called mywave.RES.
Start Delphi, then start a new project (Application), add MMSystem in the Uses clause like this:
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, MMSystem;
You cannot play sounds without the MMSystem unit, in other words PlaySound will not work without the MMSystem unit.
Look for {$R *.DFM} in Unit1 and add {$R MYWAVE.RES} next to it.
Next drop a Button onto your Form, double-click on the Button and add this code:
PlaySound(PChar('TheLaser'), hInstance, snd_Sync or snd_Resource);
Note we use 'TheLaser' to call the wave file.
Run the program and then click on the Button, you should now here the
Laser.wav file.
Your complete unit should look like this:
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, MMSystem;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var Form1: TForm1;
implementation
{$R *.DFM} {$R MYWAVE.RES}
procedure TForm1.Button1Click(Sender: TObject);
begin
PlaySound(PChar('TheLaser'), hInstance, snd_Sync
or snd_Resource);
end;
end.