The following example should open any device. I have taken the code from the attached files, but have stripped it down to make it simpler. Note the lpstrElementName, which is used to specify the filename. Note that id of the device (which returned from by the MCI_OPEN call) is stored in a variable called pDevice, and is used on all subsequent calls to the open device. Also note, the MCI_WAIT flag, which will cause the mciSendCommand not to return until it has completed.
Notice that 'lpstrDeviceType = MCI_ALL_DEVICE_ID'. This will open anything, including movies (a window will be created for these if you don't specify one). You can specify specific devices if you wish, such as MCI_DEVTYPE_WAVEFORM_AUDIO (wave) or MCI_DEVTYPE_SEQUENCER (midi), but I do not know the specific type for mp3 (if anyone knows - please tell me), although MCI_ALL_DEVICE_ID seems to work for 2000 & XP.
I have also attached a C++ AudioPlayer class, which makes use of the code snippets below. The class holds the device id privately, and provides open, play, stop, pause methods etc. There is also a errStr() method to return a string describing the error (if any) caused by the last method called; it will return an empty string if there is no error. Any error is reset after each method call. Also note that the pause() method actually calls stop(), but stores the fact that it has been paused. This is to simulate pause, because the midi device does not support an MCI PAUSE command. Finally, I am using a string class called AnsiString. This is C++ Builder speak, but it should be easy to replace this with std:string or char* (also remove #include <vcl.h>).
Code:
// Device id
MCIDEVICEID pDevice = 0;
// Setup open params block
MCI_OPEN_PARMS op;
op.dwCallback = 0;
op.lpstrDeviceType = (char*)MCI_ALL_DEVICE_ID;
op.lpstrElementName = "C:\Filename.here";
op.lpstrAlias = 0;
// Send command
if ( mciSendCommand( 0, MCI_OPEN,
MCI_OPEN_ELEMENT | MCI_WAIT,
(DWORD)&op) == 0)
{
// Success on open
pDevice = op.wDeviceID;
}
else
{
// FAIL
}
The following will play an open device of id 'pDevice'
Code:
CI_PLAY_PARMS pp;
pp.dwCallback = 0;
if ( mciSendCommand(pDevice, MCI_PLAY, MCI_WAIT, (DWORD)&pp) == 0)
{
// SUCCESS
}
else
{
// FAIL
}
The following will stop a playing device, but will not close it.
Code:
MCI_GENERIC_PARMS gp;
gp.dwCallback = 0;
if (mciSendCommand(pDevice, MCI_STOP, MCI_WAIT, (DWORD)&gp) == 0);
{
// SUCCESS
}
else
{
// FAIL
}
And the following will close the device, after which the pDevice id is 'dead'.
Code:
MCI_GENERIC_PARMS gp;
gp.dwCallback = 0;
if ( mciSendCommand(pDevice, MCI_CLOSE, MCI_WAIT, (DWORD)&gp) == 0)
{
// SUCCESS
pDevice = 0;
}
else
{
// FAIL
}