I use this code to play a MIDI file for my game, but I can not hear any sound from my speakers. Would you help me? It’s kind of an emergency, please…
My speakers are turned on 😉
[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;
}
}
I am not really sure about your implementation of winmm.dll but I have a tested and working code for it.
I got the source code from this open source project: Tea Timer.
The implementation of the code is pretty straight forward as below. Hope it helps.
EDIT: This is how you play and stop a music file: