Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am using the code below to play my background music Midi (.mid) files (for little game) and I ran into a problem I can't seem to figue out...

 

[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 doesn't work (the midi file never repeats) ....

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 midi's 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 can't 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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...