Thread: MCI_PLAY for midis

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    5

    MCI_PLAY for midis

    Ok, i got it to play a midi in c++ console app using this...


    [CODE]
    DWORD err;
    MCI_OPEN_PARMS midiParams;

    midiParams.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_SEQUENCER;

    midiParams.lpstrElementName = "song.mid";
    int soundon=1;

    if(soundon==1)
    {

    if ((err = mciSendCommand(0, MCI_OPEN, MCI_NOTIFY |MCI_OPEN_ELEMENT|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID, (DWORD)(LPVOID)&midiParams)))
    {
    /* Error */
    }

    if ((err = mciSendCommand(midiParams.wDeviceID, MCI_PLAY,0, (DWORD)(LPVOID)&midiParams)))
    {
    /* Error */
    }
    }


    And i will close it at the end of my game loop, because i want it to be async. but my question is how do I get some type of return to let me know that the song is finished, or even how do i loop the song forever? I don't understand the concept of dwReturn and Flags.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You have three options:
    [list=1][*] Create a window to receive the MM_MCINOTIFY message. Replay when the song is finished.[*] Create a thread and use the MCI_WAIT flag. Loop when the song is finished.[*] Poll regularly for the status of the device. Replay when the status indicates the song is finished.[/list=1]

    Here is sample code to implement option no. 2. You call loopMIDI when you want to play a song in a loop and stopMIDI when you want the looping to finish.

    The loopMIDI function creates a thread that simply plays the song over and over until the g_bContinue flag is set to false.

    Code:
    static volatile BOOL g_bContinue;
    
    static DWORD WINAPI loopMIDIThread(LPVOID szMIDIFileName)
    {
        UINT wDeviceID;
        DWORD dwReturn;
        MCI_OPEN_PARMS mciOpenParms      = { 0 };
        MCI_PLAY_PARMS mciPlayParms      = { 0 };
    
        mciOpenParms.lpstrDeviceType  = (LPCSTR) MCI_DEVTYPE_SEQUENCER;
        mciOpenParms.lpstrElementName = (LPCSTR) szMIDIFileName;
    
        if ((dwReturn = mciSendCommand(NULL, MCI_OPEN,
            MCI_OPEN_TYPE | MCI_OPEN_ELEMENT | MCI_OPEN_TYPE_ID,
            (DWORD_PTR) &mciOpenParms)))
        {
            return dwReturn;
        }
    
        wDeviceID = mciOpenParms.wDeviceID;
    
        while (g_bContinue)
        {
            if ((dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_WAIT, 
                (DWORD_PTR) &mciPlayParms)))
            {
                mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
                return dwReturn;
            }
        }
    
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
    
        return 0;
    }
    
    VOID loopMIDI(LPSTR szMIDIFileName)
    {
    	DWORD dwThreadId;
    	HANDLE hThread;
    
    	g_bContinue = TRUE;
    
    	hThread = CreateThread(NULL, 0, loopMIDIThread, szMIDIFileName, 0, &dwThreadId);
    	CloseHandle(hThread);
    }
    
    VOID stopMIDI(VOID)
    {
    	g_bContinue = FALSE;
    }

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    5
    thanks a lot, i'll give it a try

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Next time, actually use the code tags properly, not just enough to make the error message go away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed