Stopping and playing a midi file

eagleeye 0 Tallied Votes 848 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;
}
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.