This is the MCI control code that I wrote for my UglyCD
player. It is fairly full featured, but if you need more, feel free to modify it to your needs. I
have neglected the error checking code, and every call to mciSendCommand should really be checked for
its return value. As usual, this code is usable at your own risk. If you have any questions, you are
always free to ask.
-BossHogg
#ifndef MCI_CONTROL
#define MCI_CONTROL 1
class MCIControl
{
public:
MCIControl();
~MCIControl();
int GetNumberOfTracks();
void Resume();
void Pause();
void Play();
void Stop();
void OpenDoor();
void CloseDoor();
void Goto(int track,int minute, int second);
int GetCurrentTrack();
int GetCurrentMinute();
int GetCurrentSecond();
protected:
void Init();
void SetTimeFormat();
void GetPosition(BYTE* track,BYTE* min,BYTE* sec);
private:
MCIDEVICEID   itsMCIDevice;
};
#endif
#include "windows.h"
#include "MCIControl.h"
MCIControl::MCIControl() :
itsMCIDevice(0)
{
Init();
SetTimeFormat();
}
MCIControl::~MCIControl()
{
MCI_GENERIC_PARMS Info;
Info.dwCallback=0;
mciSendCommand(itsMCIDevice, MCI_CLOSE, MCI_NOTIFY, DWORD(&Info));
}
void MCIControl::Resume()
{
MCI_PLAY_PARMS Info;
BYTE track,minute,second;
GetPosition(&track,&minute,&second);
Info.dwTo=0;
Info.dwCallback=0;
Info.dwFrom = MCI_MAKE_TMSF(track,minute,second,0);
mciSendCommand(itsMCIDevice, MCI_PLAY, MCI_FROM|MCI_NOTIFY, DWORD(&Info));
}
void MCIControl::Pause()
{
MCI_GENERIC_PARMS Info;
Info.dwCallback = 0;
mciSendCommand(itsMCIDevice, MCI_PAUSE, MCI_NOTIFY, DWORD(&Info));
}
void MCIControl::Goto(int track,int minute, int second)
{
MCI_PLAY_PARMS Info;
Info.dwCallback=0;
Info.dwTo=0;
Info.dwFrom = MCI_MAKE_TMSF(track,minute,second,0);
mciSendCommand(itsMCIDevice, MCI_PLAY, MCI_FROM|MCI_NOTIFY, DWORD(&Info));
}
void MCIControl::Play()
{
MCI_PLAY_PARMS Info;
Info.dwCallback=0;
Info.dwTo=0;
Info.dwFrom = MCI_MAKE_TMSF(0,0,0,0);
mciSendCommand(itsMCIDevice, MCI_PLAY, MCI_FROM|MCI_NOTIFY, DWORD(&Info));
}
void MCIControl::Stop()
{
MCI_GENERIC_PARMS Info;
Info.dwCallback = 0;
mciSendCommand(itsMCIDevice, MCI_STOP, MCI_NOTIFY, DWORD(&Info));
}
void MCIControl::OpenDoor()
{
MCI_STATUS_PARMS Info;
Info.dwCallback=0;
Info.dwItem=0;
Info.dwReturn=0;
Info.dwTrack=0;
mciSendCommand(itsMCIDevice, MCI_SET, MCI_SET_DOOR_OPEN, DWORD(&Info));
}
void MCIControl::CloseDoor()
{
MCI_STATUS_PARMS Info;
Info.dwCallback=0;
Info.dwItem=0;
Info.dwReturn=0;
Info.dwTrack=0;
mciSendCommand(itsMCIDevice, MCI_SET, MCI_SET_DOOR_CLOSED, DWORD(&Info));
}
int MCIControl::GetCurrentTrack()
{
BYTE track;
GetPosition(&track,NULL,NULL);
return track;
}
int MCIControl::GetCurrentMinute()
{
BYTE minute;
GetPosition(NULL,&minute,NULL);
return minute;
}
int MCIControl::GetCurrentSecond()
{
BYTE second;
GetPosition(NULL,NULL,&second);
return second;
}
int MCIControl::GetNumberOfTracks()
{
MCI_STATUS_PARMS Info;
Info.dwCallback = 0;
Info.dwReturn = 0;
Info.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
Info.dwTrack = 0;
mciSendCommand(itsMCIDevice,MCI_STATUS,MCI_STATUS_ITEM,DWORD(&Info));
return (int)Info.dwReturn;
}
void MCIControl::GetPosition(BYTE* track,BYTE* min,BYTE* sec)
{
MCI_STATUS_PARMS Info;
DWORD MSF;
Info.dwCallback=0;
Info.dwReturn=0;
Info.dwTrack=0;
Info.dwItem = MCI_STATUS_POSITION;
mciSendCommand(itsMCIDevice, MCI_STATUS, MCI_STATUS_ITEM, DWORD(&Info));
MSF = Info.dwReturn;
if (track)
*track = MCI_MSF_MINUTE(MSF);
if (min)
*min = MCI_MSF_SECOND(MSF);
if (sec)
*sec = MCI_MSF_FRAME(MSF);
}
void MCIControl::SetTimeFormat()
{
MCI_SET_PARMS Info;
Info.dwCallback=0;
Info.dwTimeFormat=MCI_FORMAT_TMSF;
Info.dwAudio=0;
mciSendCommand(itsMCIDevice, MCI_SET, MCI_SET_TIME_FORMAT, DWORD(&Info));
}
void MCIControl::Init()
{
MCI_OPEN_PARMS Info;
Info.dwCallback=0;
Info.lpstrAlias=0;
Info.lpstrElementName=0;
Info.wDeviceID=0;
Info.lpstrDeviceType=MAKEINTRESOURCE(MCI_DEVTYPE_CD_AUDIO);
mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID, DWORD(&Info));
itsMCIDevice = Info.wDeviceID;
}
|