How to play a MIDI

G

Guest

Hello, all:

I'm using Excel 2003. I've written a Jeopardy game, and just for a bit of
fun, I want to be able to play a MIDI of the final jeopardy theme. The MIDI
is called finaljeopardy.mid.

I got the following code from a book written by John Walkenbach
(www.j-walk.com). I figure I stand a better chance of getting a response
here than I do from him.

************************************************
Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As
String) As Long

Sub PlayMIDI()
MIDIFile = "finaljeopardy.mid"
MIDIFile = ThisWorkbook.Path & "\" & MIDIFile
mciExecute ("play" & MIDIFile)
End Sub

Sub StopMIDI()
MIDIFile = "finaljeopardy.mid"
MIDIFile = ThisWorkbook.Path & "\" & MIDIFile
mciExecute ("stop" & MIDIFile)
End Sub
**********************************************************
I want to have a toggle button which will alternately start and stop the
MIDI, but I can't get it to work. I tried using individual buttons (created
from textboxes) and assigning the StartMIDI and StopMIDI procedures to them
as macros, but I get a "The driver cannot recognize the specified command"
error message box (from Excel, not from VBA).

Qhestions I have:
1. I have the code stored in Module1 of the workbook. Is this the correct
location, or should it be stored on the sheet where I have the buttons?
2. Where do I need to put the MIDI file in order for the code to access it?
3. How can I use a toggle button instead of two individual buttons?

You can probably tell that this is over my head, but I'm hoping some of you
can help me out by showing me how the code should be set up and answering my
questions.

Thanks in advance,
MARTY
 
G

Guest

I will confess at the start I have no experience working with MIDI files from
VBA or with the dll file winmm.dll, but here are some things to check:

Check the format of the mciExecute function parameter. Since you have
"play" & Midifile the resulting command string would be something like
"playC:\Documents and Settings\..." - in other words, no space between "play"
and the path: doesn't seem likely to me. More reasonable would be "play " &
Midifile (note the space). I think this is why you are getting the message:
the driver (i.e. winmm.dll) cannot recognize the command you are sending it.

Module1 will work fine for your subs - as long as you assign your button to
the correct macro, it will run the sub.

Since you are setting your MIDI file path in the code, you already specify
where it should be. Your code says
MIDIFile = "finaljeopardy.mid"
MIDIFile = ThisWorkbook.Path & "\" & MIDIFile
So your MIDI file (finaljeopardy.mid) needs to be in the same folder as
where your workbook is saved, unless you modify this code.

Finally, regarding the toggle: In Module1 you should be able to assign (in
the declarations section) a public variable to keep track of the presses:

Public ToggleState as Boolean

Then, along with PlayMIDI and StopMIDI make a new Sub:

Sub ToggleMIDI()

ToggleState = Not ToggleState
If ToggleState Then PlayMIDI Else StopMIDI

End Sub

Make your button run this sub: it will "flip" the value of ToggleState every
time it is run and use the result to decide whether to play or stop the tune.

Hope you get it working....
K Dales
 
G

Guest

K-

Thanks for the information. I added the space as you suggested but now I'm
getting a different error: "The specified device is not open or is not
recognized by MCI."

I don't know what MCI is. Any thoughts?

MARTY
 
G

Guest

MCI is the sound driver for MIDI - as in mciexecute. As I stated, I am no
expert on the MIDI driver, but it sounds like a hardware or device driver
issue. The 'specified device' would likely mean that either the sound card
or the driver used to communicate with it are not being found or recognized.
Just to cover the bases: are you sure you have a sound card capable of
playing the midi file? If not, that would explain it - but it would be a
rare machine these days that would not have it.

I did a bit of research and it is possible it is a setup issue - the address
below leads to a knowledge base article that discusses the same error message
(but in a different context) and shows how to make sure you have the MCI
sound driver properly installed:
http://support.microsoft.com/?kbid=127858

Beyond that, I am afraid you will need to find someone more familiar with
the MCI driver.

But I will also say - you are on the right track as far as your basic idea,
and all these little difficulties are fairly common when you are trying to
add some 'unusual' functionality to an application through your own coding.
So don't give up - it is coming along and you will learn a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top