Ze MIDI music file player

vegaseat 0 Tallied Votes 219 Views Share

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;  
}
evilsilver 0 Junior Poster in Training

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?

eagleeye 0 Junior Poster in Training

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)

eagleeye 0 Junior Poster in Training

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);
eagleeye 0 Junior Poster in Training

Okay that didn't work &quot is the double quotes

Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member

What do you mean?

evilsilver 0 Junior Poster in Training

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.........)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.