embed a midi file

How can I embed a midi file into my exe. I want to use the midiaplayer component but I want the midi file to compiled within the exe.
Thank you,

Beautiful! Thank you tonqalite. Now how to I cause the resource file to "capture" the midi?
Paul :)
Who is Participating?
tongaliteConnect With a Mentor Commented:
you can compile the mid file into a Resource
file then use ResourceStream  to play the file
with TMediaplayer...

//code snippet


{$R *.DFM}
{$R myres.res}

procedure TForm1.Button1Click(Sender: TObject);



 restr := TResourceStream.Create(hInstance, 'MYMIDI', RT_RCDATA);

 fileStr := TFileStream.Create('C:\temp.mid',fmCreate or fmOpenWrite);
 fileStr.CopyFrom(restr, restr.size);

     MediaPlayer1.filename := 'C:\temp.mid';


If you need any help with the resource file just ask.
Have fun
annasAuthor Commented:
tonqualite how do i write the Resource file so that it will include my midi file?
Paul :)
Hi Paul,

You do it like so:

Put the midi file in your project directory.

Open a simple text editor (notepade) and add your resource line in this manner:

[Name you'll refer to in program] [filetype] [actual file in quotes]

It will look like this

MyMidi MIDI "MidiFile.mid"

...then save and close the text file. Rename the text file to whatever you like and make the extension .rc

Then shell to Dos and run the resource compiler, brcc32 with the file name of the file you just created:

brcc32 MidiRes.rc (for example)

That compiles it, which creates the .res file, and now it is ready to be included in your program as tongalite has shown.

Here is a site with an example similar to what I said:



Hi annas...
Here is some useful info I put together from various sources plus my own section "Setting up a Resource Workshop"
hope thia helps.
tongalite. sorry it's taken so long to get back to you :)

RESOURCE FILES .tongalite 2001

Bitmaps, Wav, MIDI, Icons, Cursors, String tables... Every Windows program uses resources. Resources are those elements of a program that support the program but are not executable code. In this article, we will walk through some examples of the use of bitmaps, icons and cursors from resources.

Placing resources in the .exe file has two main advantages:
- The resources can be accessed more quickly because it takes less time to locate a resource in the executable file than it does to load it from a disk file.
- The program file and resources can be contained in a single unit (the .exe file) without the need for a lot of supporting files.

The Image Editor
First off all, we need to create a resource file. Default extension for resource files is .RES. Resource files can be created with Delphi's Image Editor. Read in Help files on working with the Image Editor.

 Step 1, Setting up a resource workshop
First create a new folder and name it RESOURCES (this will be the base for your resources workshop, and all future resource files that you need to create.)
Next, from your Delphi bin locate and copy these two files: BRCC32 .exe and Rw32core.dll. Paste them into your newly created RESOURCES folder.
Also create a shortcut to Windows Notepad in the folder. (you will use this to create .rc files used to make resource files) You now have the tools at your disposal to begin creating Res files. The next step is to create a .rc file which will tell the resource compiler (BRCC32 .exe) what to look for.
Step 2, Making .rc files
Open Notepad and type in the the details of the file you wish to compile into a res file. This could be a Bitmap, a wav, Midi,icon, jpeg, avi or whatever. For this first example we will use a WAV file. Let's assume you have a Wav file called Mywave. You would type this into Notepad: TEST WAVE "Mywave.wav" The first reference <TEST> is the name you will refer to in your code. The 2nd parameter <WAVE> could be anything, and the 3rd parameter "MyWave.wav" is the actual name of your file with its extension. (another example could be TEST BITMAP "MyBitmap.bmp") OK, you have typed your info in to Notepad, now save the text file as (example) Myres.rc. Now make sure your file to be converted is in the RESOURCES folder. then drag the .rc file onto the BRCC32.exe icon, your resource file will be automatically created.
Step3, Adding the res file to your project:  
export the file < Myres.Res > to your Delphi Project folder. Open a new Delphi project and add this compiler directive: {$R Myres.RES} beneath the Implementation and under {$R *.DFM}. Also add MMSystem to your uses clause.Save your project to the new project folder where you exported the RES file earlier.
When ready to play your sounds, use the following line of code in the begin and end block (on a button click event):

PlaySound(PChar('TEST'), hInstance, snd_ASync or snd_Resource);

The TEST is the name you gave your sound in the *.RC file. Look in Help for more details on PlaySound.
This technique will make your executable a bit larger because the *.wav files are now an integral part. However, it makes it easy to add sound to your applications, and you still have no DLL or WAV files that must be kept in

Other resource types.
param1                        param2                     param3

For example, to include a bitmap in the resource file: IMAGE BITMAP MyBitmap.bmp

To include a cursor:
You use the same technique to include AVI, WAV and ICO files.

For other filetypes, you can use the type RC_DATA.

You can name the resource file anything you want, as long as it has the extension ".RES" and the filename without the extension is not the same as any unit or project filename. This is important, because by default, each Delphi project that compiles into an application has a resource file with the same name as the project file, but with the extension .RES. It's best to save the file to the same directory as your project file.

   Including resources in applications
In order to access our own resource file we have to tell Delphi to link our resource file in with our application. This is accomplished by adding a compiler directive to the source code. This directive needs to immediately follow the form directive, like the following:Do not accidentally erase {$R *.DFM} part, as this is the line of code that tells Delphi to link in the form's visual part. When you choose bitmaps for speed buttons, Image components or Button components, Delphi includes the bitmap file you chose as part of the form's resource. Delphi isolates your user interface elements into the .DFM file.To actually use the resource, you must make a few Windows API calls. Bitmaps, cursors and icons stored in RES files can be retrieved by using the API functions LoadBitmap, LoadCursor and LoadIcon respectively.
   Pictures in resources
The first example shows how to load a bitmap stored as a resource and display it in a TImage component.
  procedure TfrMain.btnCanvasPic(Sender: TObject);
var bBitmap : TBitmap;
 bBitmap := TBitmap.Create;
  bBitmap.Handle := LoadBitmap(hInstance, 'ATHENA');
  Image1.Width := bBitmap.Width;
  Image1.Height := bBitmap.Height;

Note: If the bitmap that is to be loaded is not in the resource file, the program will still run, it just won't display the bitmap. This situation can be avoided by testing to see if the bBitmap.Handle is zero after a call to LoadBitmap() and taking the appropriate steps. The try/finally part in the previous code doesn't solve this problem, it is just here to make sure that the bBitmap is destroyed and its associated memory is freed.

Another way we can use to display a bitmap from resource is as follows:

procedure TfrMain.btnLoadPicClick(Sender: TObject);

 Cursors in resources
As discussed in the previous article Screen.Cursors[] is an array of cursors supplied by Delphi. By using resource files, we can add custom cursor to the Cursors property. Unless we wish to replace any of the defaults, the best strategy is to use cursor numbers starting from 1.
procedure TfrMain.btnUseCursorClick(Sender: TObject);
 const NewCursor = 1;
 Screen.Cursors[NewCursor] :=
 Image1.Cursor := NewCursor;


Icons in Resources
If we look at Delphi's Project-Options-Application settings, we can find that Delphi supplies the default icon for a project. This icon represents the application in the Windows Explorer and when the application is minimized. We can easily change this by clicking the 'Load Icon' button.
If we want, for example, to animate the program's icon when the program is minimized than the following code will do the job.
For the animation, we need a TTimer component on a form. The code loads two icons from resource file into an array of TIcon objects; this array needs to be declared in the public part of the main form. We'll also need NrIco, that is an Integer type variable, declared in the public part. The NrIco is used to keep track of the next icon to show.
    nrIco : Integer;
  MinIcon : array[0..1] of TIcon;
procedure TfrMain.FormCreate(Sender: TObject);
procedure TfrMain.Timer1Timer(Sender: TObject);
 if IsIconic(Application.Handle) then begin
  NrIco:=(NrIco+1) mod 2;
procedure TfrMain.FormDestroy(Sender: TObject);



 Final words
Anything (well, not everything) can be put in resource files. This article has shown you how to use resources to use/display bitmap, cursor or an icon in your Delphi application.  
Note: When we save a Delphi project to the disk, Delphi automatically creates one .RES file that has the same name as the project (if nothing else, the main icon of the project is inside). Although you can alter this resource file, this is not advisable.
annasAuthor Commented:
Thank you reddarin and tongalite! With this much information I should haveno trouble including a midi (or any other file) in my Exe. Again Thank you very much!
Paul (annas)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.