?
Solved

playing midi files using MCI interface

Posted on 1998-07-25
4
Medium Priority
?
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

752 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