Solved

playing midi files using MCI interface

Posted on 1998-07-25
4
361 Views
Last Modified: 2008-02-01
Can anyone provide me with some code to play a midi file using the windows MCI interface?

I've been looking through the VC4 help files, and it all looks really complicated...

any code would be appreciated.

thanks
steb
0
Comment
Question by:steb
4 Comments
 
LVL 1

Expert Comment

by:timop
ID: 1168610
Here is some sample code:

void PlayMidi(LPCTSTR lpszFileName)
{
  MCI_OPEN_PARMS mciOpenParms;
  MCI_PLAY_PARMS mciPlayParms;
  MCIDEVICEID wDeviceID;

  mciOpenParms.lpstrDeviceType = "sequencer";
  mciOpenParms.lpstrElementName = lpszFileName;
  if ( mciSendCommand( 0, MCI_OPEN, MCI_OPEN_TYPE |  MCI_OPEN_ELEMENT, (DWORD)(LPVOID) &mciOpenParms) )
    // error
    return;

  wDeviceID = mciOpenParms.wDeviceID;
      
  mciPlayParms.dwCallback = NULL;
  if ( mciSendCommand( wDeviceID, MCI_PLAY, MCI_WAIT, (DWORD)(LPVOID)&mciPlayParms) )
    // error
    return;
            
  mciSendCommand( wDeviceID, MCI_CLOSE, 0, NULL);
}

0
 

Author Comment

by:steb
ID: 1168611
thanks for your help, but I forgot to mention the midi file needs
to be played in the background, inside quake2...
at the minute, it freezes my system.

anymore help would be much appreciated.

thanks
steb
0
 
LVL 2

Expert Comment

by:rayb
ID: 1168612
I don't think that you'll be able to do this, as Quake and Quake 2 both obtain exclusive locks on the sound hardware at game startup.  Now, if you had access to ID Software's source code, then we might be able to do something.

0
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1168613
Don't use the MCI_WAIT flag. The following code is copied from the Platform SDK documentation. Close the device (mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL)) when you receive the MM_MCINOTIFY message.

Playing a MIDI File
The following example opens a MIDI sequencer device, verifies that the MIDI mapper was selected as the output port, plays the MIDI file specified by the lpszMIDIFileName parameter, and closes the device after playback is complete.

// Plays a specified MIDI file by using MCI_OPEN and MCI_PLAY. Returns
// as soon as playback begins. The window procedure function for the
// specified window will be notified when playback is complete.
// Returns 0L on success; otherwise, it returns an MCI error code.
DWORD playMIDIFile(HWND hWndNotify, LPSTR lpszMIDIFileName)
{
    UINT wDeviceID;
    DWORD dwReturn;
    MCI_OPEN_PARMS mciOpenParms;
    MCI_PLAY_PARMS mciPlayParms;
    MCI_STATUS_PARMS mciStatusParms;
    MCI_SEQ_SET_PARMS mciSeqSetParms;

    // Open the device by specifying the device and filename.
    // MCI will attempt to choose the MIDI mapper as the output port.
    mciOpenParms.lpstrDeviceType = "sequencer";
    mciOpenParms.lpstrElementName = lpszMIDIFileName;
    if (dwReturn = mciSendCommand(NULL, MCI_OPEN,
        MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,
        (DWORD)(LPVOID) &mciOpenParms))
    {
        // Failed to open device. Don't close it; just return error.
        return (dwReturn);
    }

    // The device opened successfully; get the device ID.
    wDeviceID = mciOpenParms.wDeviceID;

    // Check if the output port is the MIDI mapper.
    mciStatusParms.dwItem = MCI_SEQ_STATUS_PORT;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_STATUS,
        MCI_STATUS_ITEM, (DWORD)(LPVOID) &mciStatusParms))
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (dwReturn);
    }

    // The output port is not the MIDI mapper.
    // Ask if the user wants to continue.
    if (LOWORD(mciStatusParms.dwReturn) != MIDI_MAPPER)
    {
        if (MessageBox(hMainWnd,
            "The MIDI mapper is not available. Continue?",
            "", MB_YESNO) == IDNO)
        {
            // User does not want to continue. Not an error;
            // just close the device and return.
            mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
            return (0L);
        }
    }

    // Begin playback. The window procedure function for the parent
    // window will be notified with an MM_MCINOTIFY message when
    // playback is complete. At this time, the window procedure closes
    // the device.
    mciPlayParms.dwCallback = (DWORD) hWndNotify;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY,
        (DWORD)(LPVOID) &mciPlayParms))
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (dwReturn);
    }

    return (0L);
}

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now