Repeat/Loop Midi Playback [C#]

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
I am using the code below to play my background music Midi (.mid) files (for little game) and I ran into a problem I cant seem to figue out...

Code:
[DllImport("winmm.dll", EntryPoint="mciSendStringA")]
private static extern long mciSendString(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback);

public static long PlayMidiFile(string MidiFile)
{
	long lRet = -1;

	if (File.Exists(MidiFile)) 
	{
		lRet = mciSendString("stop midi", "", 0, 0);
		lRet = mciSendString("close midi", "", 0, 0);
		lRet = mciSendString(("open sequencer!" 
			+ (MidiFile + " alias midi")), "", 0, 0);
		lRet = mciSendString("play midi", "", 0, 0);
		return lRet;
	}

	else
	{
		//Error Message
		return lRet;
	}
}

Thing is I want the Midi file to repeat/loop (it is the background music for my game, so it should play over and over again until the game ends)...

So I am having 3 issues:
1- Repeat/Loop: I did some reading on MSDE and it mentions that we can use a "repeat" value, so I tried to implement it as follows (only changed this one line) but it doesnt work (the midi file never repeats) ....
Code:
lRet = mciSendString("play midi", "repeat", 0, 0);
Any clues?

2- And next, almost as important when my game ends it could be in the middle of playing the MIDI file - how can I STOP the play? My program remains open (incase the user decides to restart) but the background music should end at this point...
There must be some way to pause/stop the playing of the midi song no?

3- The least important, is it possible to play two midis at the same time? (one on-top of the other)? Meaning if I run [PlayMidiFile(string MidiFile)] multiple times the last run is the music I heard (they do not super-impose) but sometimes I want them to... Is this do-able with Midi using my approach?
(because if I play them as WAV using my PlaySound() function it seems to work perfectly fine, they super-impose perfectly well - but as with Midi I cant super-impose two WAVs either... only by mixing the two have I been able to so far ahieve my desired results...)

Any ideas, hints, and help would be greatly appreciated, thanks
 
For the repeat function try
C#:
lRet = mciSendString("play midi repeat", "", 0, 0);

To stop a playing track you would send the stop command using the same deviceid (midi in this case) you used to open it.
 
Back
Top