5

I'm making a game, and I'm using midi files for the background music. Here's the code I'm currently using:

void Game::play_music()
{
    // Start the music:
    if(map.levelnumber % 2 == 0)
    {
        mciSendString(L"open MUSIC01.MID type sequencer alias Music1", NULL, 0, NULL);
        mciSendString(L"play Music1", NULL, 0, NULL);
        playing = "Music1";
    } else {
        mciSendString(L"open MUSIC02.MID type sequencer alias Music2", NULL, 0, NULL);
        mciSendString(L"play Music2", NULL, 0, NULL);
        playing = "Music2";
    }
}

This works great, except it only plays the song once through. I tried to loop it by adding "repeat" in the mci play command, but then it doesn't play anything at all. How can I loop the music?

Brian Gradin
  • 2,165
  • 1
  • 21
  • 42
  • The repeat option is [not documented](http://msdn.microsoft.com/en-us/library/windows/desktop/dd743451%28v=vs.85%29.aspx) to work for the sequencer. This can get ugly in [a hurry](http://msdn.microsoft.com/en-us/library/windows/desktop/dd757277%28v=vs.85%29.aspx), do consider using one of the many audio libraries. – Hans Passant May 30 '13 at 11:20

2 Answers2

2

I looked at the multimedia command string reference on MSDN and found out that

TCHAR tch[100];
mciSendString("status [alias] mode", tch, 100, NULL);

will put whether [alias] is playing or stopped into "tch". If it's stopped, I just use the "seek [alias] to start" command to reload the midi file. I should mention that this causes a delay while stuff is computing or whatever - so your program will freeze if you don't put this in a separate thread.

Brian Gradin
  • 2,165
  • 1
  • 21
  • 42
  • 1
    Note that all mci commands must come from the same thread if you're going the multithreading approach. – chili Nov 13 '15 at 08:32
0

I know i'm a little late but i just encountered (and solved) this issue with:

mciSendString("seek [alias] to start", tch, 100, NULL);

This works for simple .wav files, i suppose it does well with .mid too. When i want to play an opened file, i just send 2 commands:

mciSendString("seek [alias] to start",NULL,100,NULL);
mciSendString("play [alias]",NULL,0,NULL);