Many of you know how I like to use sound to make the old computer box more interesting. The midi file packs a lot of neat instrumental music. It is very simple to play, even in a console application, using the winmm.lib file that comes with just about any Windows based compiler. Here is the code. Turn up the speakers and blast the dust off the rafters!

// use mciSendString() to play a midi music file
// you can also play from pos1 to pos2, stop, pause and resume
// you have to link with the winmm.lib file, or
// in the case of Dev-C++ link with libwinmm.a via
// Project>>Project Options>>Parameters>>Add Lib>>libwinmm.a
// a Dev-C++ tested console application by  vegaseat  19dec2004

#include <iostream>  
#include <windows.h> 
#include <mmsystem.h>  // mciSendString()

using namespace std;   // std::cout, std::cin

int main()  
{  
  cout << "Looking for  onestop.mid  midi music file in" << endl;
  cout << "the windows media directory, wait just a second ...";
  
  // this midi file comes with Windows XP
  // you might have to change it to something you have 
  mciSendString("play C:/Windows/Media/onestop.mid",NULL,0,NULL);
  
  // pause the play
  //mciSendString("pause C:/Windows/Media/onestop.mid",NULL,0,NULL);

  // resume play from paused position
  //mciSendString("resume C:/Windows/Media/onestop.mid",NULL,0,NULL);
  
  cout << "\n\nPress Enter to stop ...";
  cin.get();
  // stop play, if you play again it will play from beginning
  mciSendString("stop C:/Windows/Media/onestop.mid",NULL,0,NULL);

  // play the midi file from sequence 10 to sequence 50
  //mciSendString("play C:/Windows/Media/onestop.mid from 10 to 50 notify",NULL,0,NULL);

  // almost forgot, of course you can play a .wav file too
  mciSendString("play C:/Windows/Media/ding.wav",NULL,0,NULL);
  
  return 0;  
}

Awsome man i was wondering how to do this, just one problem. i got it to play one song but i want it to swich to another one aswell, how do you get one mid file to stop and the other to start?

The answer, is rather quite logical, instead of mciSendString("play C:\\Windows\\Media\\onestop.mid",NULL,0,NULL); for starting it, and the opposite of play is, well stop so mciSendString("stop C:\\Windows\\Media\\onestop.mid",NULL,0,NULL); would stop the file. And to play another file simply go back to the start but with a different file (yes I knowI'm stating the obvious here). For an example please see http://daniweb.com/code/snippet190.html it may and most likely is not perfect but at least it works (for me at least)

A little corection for the typos

mciSendString("play C:\\Windows\\Media\\onestop.mid",NULL,0,NULL);

mciSendString("stop C:\\Windows\\Media\\onestop.mid",NULL,0,NULL);

ok so that didn't work for me at all, it does in the borland compiler but i am currently trying to move all my stuff over to the Microsoft Visual Studio 2005 and it doesn't work in that one, the only problem it keeps throughing at me is "error C2664: 'mciSendStringW' : cannot convert parameter 1 from 'const char [34]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

to fix this you need to right click on second thing in the left hand window and go down to properties on the second item in the list on the left of the window that opens expand, or click the + sign, in front of the "configuration settings" then click on general. on the right where it says "character set " then says "use unicode character set" click that on the far right a drop down arrow apears click it and go down to "Use Multi-Byte Character Set" and select that.

next you have to include the winmm.lib by going down the left hand list to linker, expand that and go down to input, on the right hand side of the window at the first line it says "additional dependencies" click to type next to it and simply type winmm.lib.

took me forever to figure this out lol my teacher had to help me and he never heard of mciSendString before lol, hope this helps at least someone lol (sorry for the lengthy discription of how to fix it, thought it would be useful to people like me who are new to the program.........)

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.