You have three options:
[list=1][*] Create a window to receive the MM_MCINOTIFY message. Replay when the song is finished.[*] Create a thread and use the MCI_WAIT flag. Loop when the song is finished.[*] Poll regularly for the status of the device. Replay when the status indicates the song is finished.[/list=1]
Here is sample code to implement option no. 2. You call loopMIDI when you want to play a song in a loop and stopMIDI when you want the looping to finish.
The loopMIDI function creates a thread that simply plays the song over and over until the g_bContinue flag is set to false.
Code:
static volatile BOOL g_bContinue;
static DWORD WINAPI loopMIDIThread(LPVOID szMIDIFileName)
{
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms = { 0 };
MCI_PLAY_PARMS mciPlayParms = { 0 };
mciOpenParms.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_SEQUENCER;
mciOpenParms.lpstrElementName = (LPCSTR) szMIDIFileName;
if ((dwReturn = mciSendCommand(NULL, MCI_OPEN,
MCI_OPEN_TYPE | MCI_OPEN_ELEMENT | MCI_OPEN_TYPE_ID,
(DWORD_PTR) &mciOpenParms)))
{
return dwReturn;
}
wDeviceID = mciOpenParms.wDeviceID;
while (g_bContinue)
{
if ((dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_WAIT,
(DWORD_PTR) &mciPlayParms)))
{
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return dwReturn;
}
}
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
return 0;
}
VOID loopMIDI(LPSTR szMIDIFileName)
{
DWORD dwThreadId;
HANDLE hThread;
g_bContinue = TRUE;
hThread = CreateThread(NULL, 0, loopMIDIThread, szMIDIFileName, 0, &dwThreadId);
CloseHandle(hThread);
}
VOID stopMIDI(VOID)
{
g_bContinue = FALSE;
}