Archived

This topic is now archived and is closed to further replies.

Squeejee

Playing .mid files in windows.

Recommended Posts

Beer Hunter    712
Something like this.

  
#include <windows.h>
#include <mmsystem.h>

namespace Midi_NS {
bool Loaded = false;
UINT ID;
};

bool LoadMidi(char *FileName)
{
using namespace Midi_NS;
MCI_OPEN_PARMS Params;
CloseMidi();
Params.lpstrDeviceType = "sequencer";
Params.lpstrElementName = FileName;
if(mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD) &Params))
return false;
else
{
ID = Params.wDeviceID;
Loaded = true;
return true;
}
}

void PlayMidi(HWND CallbackWindow)
{
using namespace Midi_NS;
MCI_PLAY_PARMS Params;
Params.dwCallback = (DWORD) CallbackWindow;
Params.dwFrom = 0;
if(Loaded) mciSendCommand(ID, MCI_PLAY, MCI_NOTIFY|MCI_FROM, (DWORD) &Params);
}

void StopMidi(void)
{
using namespace Midi_NS;
DWORD dummy;
if(Loaded) mciSendCommand(ID, MCI_STOP, 0, (DWORD) &dummy);
}

void CloseMidi(void)
{
using namespace Midi_NS;
DWORD dummy;
if(Loaded) mciSendCommand(ID, MCI_CLOSE, MCI_WAIT, (DWORD) &dummy);
Loaded = false;
}


In the window procedure for the callback window, check if the message is MM_MCINOTIFY, and the wParam is MCI_NOTIFY_SUCCESSFUL. If both conditions are met, call PlayMidi again. It's possible to automate that process, but I didn't want that to get in the way of the important code in this example.

Edited by - Beer Hunter on November 12, 2001 1:30:14 AM

Share this post


Link to post
Share on other sites