embed a midi file

Posted on 2002-03-04
Last Modified: 2007-12-19
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 :)
Question by:annas
  • 2
  • 2

Accepted Solution

tongalite earned 50 total points
ID: 6840604
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

Author Comment

ID: 6842596
tonqualite how do i write the Resource file so that it will include my midi file?
Paul :)

Expert Comment

ID: 6853635
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:


Expert Comment

ID: 6853648

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.

Author Comment

ID: 6854031
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)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now