Link to home
Start Free TrialLog in
Avatar of wrongwaysg
wrongwaysg

asked on

Standalone midi or music program

How do I write a vb control to play background midi which compiles into a standalone exe? The midi or music score is embedded in the main executable.
Avatar of J-Man
J-Man

(1) First goto the Project menu and select Components.  Then put a check in the box for Microsoft MultiMedia Control.  You now have a MMC icon in your controls toolbox.

(2) Place a MMC onto your main form.

(3) Set the filename property of the MMC to the midi file you wish to play.  If you put the name in at design time, it will embed the file into your executable when you compile.

(4) In the Form_Load sub place the following:
MMControl1.visible = False 'You could just set this at design time instead of when you load.
MMControl1.Command = "Open" 'I'm not sure if you need this line. Try with and without.
MMControl1.Command = "Play"

(5) Compile the exe and run.
Avatar of wrongwaysg

ASKER

Hi J-Man
I tried using the MMC. I did not embedded the midi file into the exe. The minute I deleted the original midi file it did not work.
My bad; the MMC does NOT embed the file (like a picturebox would.)
There are basically 5 options now:
(1) Keep the midi file in the exe folder and reference it from there in the mmc.filename property.  But you would have to distribute the midi file with the exe.  This shouldn't be too much of a problem as midi files are very small.
(2) Create a resource file with the midi file as a custom resource.  This adds no advantage over option (1) however and would only complicate things.
(3) Use the commondialog control and in the form_load sub ask the user to select the midi file to play.  (This way you can use the same exe for any midi file, but the file would still have to be on the computer harddrive or cd-rom.)
(4) Use an OLE container control and make it type embedded (OLEType = 1-Embedded).  Then drop the data from the OLE container into the MMC.  I'm not too familiar with this control and cannot say for sure that the file will actually be embedded in the exe or not.
(5) Copy & Paste the text from the .mid file into a string variable during form_load sub, then use winmm.dll functions to play the midi file manually.  This is fairly complex also and would require some intensive learning of the winmm.dll functions and the midi format, also you would have to do all your own timing which gets even messier as the timer control is not fast enough - timeGetTime function must be used instead and converted into "ticks" which is dependent on several variables in the midi file.

It seems that option (1) or (3) would be the easiest way to go, options (4) and (5) are harder, but would probably do what you want.

By the way, the only code you actually need for option (1) is:

Option Explicit
Private Sub Form_Load()
    MMControl1.Command = "Open" 'This IS necessary.
    MMControl1.Command = "Play" 'Play the midi sequence.
End Sub
Private Sub Form_Unload(Cancel As Integer)
    MMControl1.Command = "Close" 'Close when not needed.  Save system resources.
End Sub
P.S.
I can provide code for option (5), however it is pretty long and would probably be easier just to e-mail you the source files or put them on my webspace.
Hi J-Man
Accepted your comments on using a custom resource file.
ASKER CERTIFIED SOLUTION
Avatar of J-Man
J-Man

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial