Stopping and playing a midi file

eagleeye 0 Tallied Votes 872 Views Share

A program that plays a MIDI file and stops it, and than starts a different MIDI file. The same concept applies to .wav files mciSendString("play the_file",NULL,0,NULL); starts playing it, and as expected mciSendString("stop the_file",NULL,0,NULL); stops playing it.

// use mciSendString() to play and stop a midi music file
// 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++  console application

#include <iostream>
#include <windows.h>
#include <mmsystem.h>  // mciSendString()
#include <conio.h>
using namespace std;   // std::cout, std::cin

int main()
{
char wait;
        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);
        
        cout << endl << "Enter any letter to change to the next file.";
        getch(); // wait
        cout << endl << "Stopping onestop.mid" << endl;
        mciSendString("stop C:\\Windows\\Media\\onestop.mid",NULL,0,NULL);
                       // stops the file ^
        cout << "Looking for  flourish.mid  midi music file in" << endl;
        cout << "the windows media directory, wait just a second ...";
       // this midi file comes with Windows XP also
        mciSendString("play C:\\Windows\\Media\\flourish.mid",NULL,0,NULL);
        
        getch();  // wait
        return 0;
}

Dani AI

Generated

Short summary and fixes for the common problems raised in this thread (good starter example by , questions from , and others).

The usual causes of the errors shown here are (1) wrong include syntax/order, (2) not linking the multimedia library, and (3) relying on stopping by filename instead of using an MCI alias. Use <windows.h> before <mmsystem.h>, and make sure the Windows SDK is installed so that header exists. If the linker complains about impmciSendString... then link the WinMM library (add winmm.lib to Linker->Input or add a pragma in code). On MinGW/Dev-C++ add -lwinmm (libwinmm.a). If you see Unicode vs ANSI symbols (mciSendStringW vs mciSendStringA), either call the A/W variant explicitly or supply the correct string type.

A safer pattern is to open the file with an alias, check the MCI return value with mciGetErrorString, then play/stop/close by alias. Example (ANSI, explicit linking via pragma):

#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
#include <iostream>

static void PrintMciError(MCIERROR e) {
  char buf[256]={0};
  if (e) mciGetErrorStringA(e, buf, sizeof(buf));
  std::cerr << "MCI error: " << buf << " (" << e << ")\n";
}

int main() {
  MCIERROR err = mciSendStringA("open \"C:\\Path\\file.mid\" type sequencer alias song", NULL,0,NULL);
  if (err) { PrintMciError(err); return 1; }
  mciSendStringA("play song", NULL,0,NULL);
  /* stop/close later */
  mciSendStringA("stop song", NULL,0,NULL);
  mciSendStringA("close song", NULL,0,NULL);
  return 0;
}

Notes: always call "close" to release the device; use "play song wait" for synchronous play or MCI_NOTIFY for callbacks; escape backslashes or prefer forward slashes in paths. For simple WAV playback consider PlaySound; for more advanced, use modern APIs (DirectSound/XAudio2/SDL). This addresses the include/link errors reported by and and improves reliability over stopping by filename as in the original example.

bumsfeld 413 Nearly a Posting Virtuoso

These looks very much like the code VegaSeat wrote earlier?

eagleeye 0 Junior Poster in Training

It essentailly is, but there were several people who couldn't quite figure out on how to stop the the midi file.

VASH007 0 Newbie Poster

I have C++ 2005 Express Edition.
So when i use this code just to play a sound ".wav" file, the program doesn't work and it says " 'mmsystem': No such file or directory "

#include <iostream>
using namespace std;
#include <mmsystem> 
void main()
{
    use mciSendString();
    mciSendString("play 1",NULL,0,NULL);
system("pause");
}

??

Yuichi 0 Newbie Poster

erm i tried to used the code u gave earlier but i got error. Iam using visual studio 2008.

tamtamtaramtam 0 Newbie Poster

I tried the code written above, exactly the same thing, but im still getting a linker error :

unresolved external symbol __imp__mciSendStringA@16 referenced in function _main

can anyone help me with that ???
i added the winmm.lib in project > porperties > linker > additional library directories
note that im using Visual Studio 2005

thank u

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.