Help with a midi player

Hello,

I'm making a console text game and i would like it to play midis during the game.

I found this sample code on:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_playing_a_midi_file.asp

So i made a program from scratch to test that code.

At first, it gave the following error:
"midiplayer.cpp(45) : error C2065: 'hMainWnd' : undeclared identifier"

Then, I asked some ppl in a C++ irc channel and they told to try to put this:
"HWND hMainWnd = GetActiveWindow();"

I inserted this and the program gave a warning but it run.
But every time I run it, it says that it cant find a midi mapper.
The warning is:
"midiplayer.cpp(15) : warning C4101: 'mciSeqSetParms' : unreferenced local variable"

I'm using VC++ .NET 2003...
Could any1 help me out, what should i do now?

The whole code is attached...

Thanks,
[980 byte] By [Kalamat] at [2007-11-19 10:50:51]
# 1 Re: Help with a midi player
Are you able to play midi files at all on your PC?
e.g. with other programms?
What kind of soundcard do you use?
Are the drivers installed correctly?
and finally, check out this about midi-mapper
http://math.boisestate.edu/gas/midi/html/midi_mapper.html
blueday54555 at 2007-11-9 0:46:17 >
# 2 Re: Help with a midi player
Try with this code for midiplayer.cpp

#include "stdafx.h"
//#using <mscorlib.dll>

//using namespace std;

HWND hMainWnd = GetActiveWindow();

DWORD playMIDIFile(HWND hWndNotify, LPSTR lpszMIDIFileName)
{
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
MCI_PLAY_PARMS mciPlayParms;
MCI_STATUS_PARMS mciStatusParms;
MCI_SEQ_SET_PARMS mciSeqSetParms;

// Open the device by specifying the device and filename.
// MCI will attempt to choose the MIDI mapper as the output port.
mciOpenParms.lpstrDeviceType = "sequencer";
mciOpenParms.dwCallback=0;
mciOpenParms.lpstrElementName = lpszMIDIFileName;
if (dwReturn = mciSendCommand(NULL, MCI_OPEN,
MCI_OPEN_TYPE | MCI_OPEN_ELEMENT | MCI_WAIT,
(DWORD)(LPVOID) &mciOpenParms))
{
// Failed to open device. Don't close it; just return error.
return (dwReturn);
}

// The device opened successfully; get the device ID.
wDeviceID = mciOpenParms.wDeviceID;

// Check if the output port is the MIDI mapper.
mciStatusParms.dwItem = MCI_SEQ_STATUS_PORT;
if (dwReturn = mciSendCommand(wDeviceID, MCI_STATUS,
MCI_STATUS_ITEM, (DWORD)(LPVOID) &mciStatusParms))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}

// The output port is not the MIDI mapper.
// Ask if the user wants to continue.
if (LOWORD(mciStatusParms.dwReturn) != WORD(MIDI_MAPPER)) // MIDI_MAPPER==-1, so you must convert it to WORD
{
if (MessageBox(hMainWnd,
"The MIDI mapper is not available. Continue?",
"", MB_YESNO) == IDNO)
{
// User does not want to continue. Not an error;
// just close the device and return.
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (0L);
}
}

// Begin playback. The window procedure function for the parent
// window will be notified with an MM_MCINOTIFY message when
// playback is complete. At this time, the window procedure closes
// the device.
mciPlayParms.dwCallback = (DWORD) hWndNotify;
if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_WAIT,
(DWORD)(LPVOID) &mciPlayParms)) // request to wait
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return (dwReturn);
}

return (0L);
}

int main()
{
playMIDIFile(0,"C:\\hangar.mid");
return 0;
}
SuperKoko at 2007-11-9 0:47:17 >
# 3 Re: Help with a midi player
Superkoko,

I tested with your code and it worked just fine.

What did you changed from the original code?
And im just having a small problem now, where can i get a list of the open commands? Because when I play the midi file, it only lets the program code continue when it ends and I would like it to play with the program instead of waiting the midi file to end.

Thanks very much,

Victor Freire
Kalamat at 2007-11-9 0:48:19 >
# 4 Re: Help with a midi player
I solved the problem, in the playback part, where there is the mci command "MCI_WAIT", I put a 0 in its place so that now, it immediately returns the command to the user instead of waiting the midi file end.

Thanks,

Victor Freire
Kalamat at 2007-11-9 0:49:18 >