MCI(Media Control Interface)是提供的控制多媒体设备的高层、通用的命令接口。它提供一组与设备无关的函数和命令,可有效地控制多媒体设备。 使用MCI我们可以十分容易的对多媒体进行操作,不需要了解多媒体设备的工作原理和调用底层函数库的方法,这样就极大的减轻了开发的难度和负担。这里举一个WAV/MIDI播放器的小例子,希望以后能用到。
1、首先建立一个基于对话框的工程MIDIPlay,在对话框添加7个按钮,按钮ID分别为ID_OPEN、ID_PLAY、ID_PAUSE、ID_PREVIOUS、ID_NEXT、ID_STOP、ID_EXIT,另外加一个Slider控件。
2、为CMIDIPlayDlg类加入成员变量:
CString strFileExt;
DWORD dCurrentPosition;
unsigned long m_dLength;
bool isPause;
WORD m_wDeviceID;
CString strFileName;
3、各个按钮的消息相应函数代码如下
void CMIDIPlayDlg::OnOpen()
{
// TODO: Add your control notification handler code here
CFileDialog file(TRUE,"","",OFN_FILEMUSTEXIST,"(*.mid及*.wav文件)||*.wav||*.mid");
if (file.DoModal()==IDOK)
{
strFileName=file.GetFileName();
strFileExt=file.GetFileExt();
}
this -> SetWindowText("请单击Play按钮,欣赏"+strFileName);
}
void CMIDIPlayDlg::OnPlay()
{
// TODO: Add your control notification handler code here
isPause=true;
SetTimer(1,33,NULL);
mciSendCommand(m_wDeviceID,MCI_CLOSE,0,NULL);
MCI_OPEN_PARMS mciOpen;
MCI_PLAY_PARMS mciPlay;
mciOpen.lpstrElementName=strFileName.GetBuffer(strFileName.GetLength());
mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpen);
m_wDeviceID=mciOpen.wDeviceID;
MCI_STATUS_PARMS mciStatusParms;
mciStatusParms.dwItem=MCI_STATUS_LENGTH;
mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_WAIT|MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);
m_dLength=mciStatusParms.dwReturn;
mciSendCommand(m_wDeviceID,MCI_PLAY,0,(DWORD)(LPVOID)&mciPlay);
m_position.SetRange(0,m_dLength);
m_position.SetPos(0);
}
void CMIDIPlayDlg::OnStop()
{
// TODO: Add your control notification handler code here
mciSendCommand(m_wDeviceID,MCI_STOP,0,NULL);
//m_position.SetPos(0);
}
void CMIDIPlayDlg::OnPause()
{
// TODO: Add your control notification handler code here
if(isPause)
{
isPause=FALSE;
MCI_GENERIC_PARMS mciPause;
mciSendCommand(m_wDeviceID,MCI_PAUSE,0,(DWORD)(LPVOID)&mciPause);
}
else
{
isPause=TRUE;
if(strFileExt=="mid"||strFileExt=="MID")
{
MCI_STATUS_PARMS mciStatusParms;
MCI_PLAY_PARMS mciPlayParms;
mciStatusParms.dwItem=MCI_STATUS_POSITION;
mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);
}
else
{
MCI_GENERIC_PARMS mciResume;
mciSendCommand(m_wDeviceID,MCI_RESUME,0,(DWORD)(LPVOID)&mciResume);
}
}
}
void CMIDIPlayDlg::OnPrevious()
{
// TODO: Add your control notification handler code here
isPause=true;
MCI_STATUS_PARMS mciStatusParms;
MCI_PLAY_PARMS mciPlayParms;
mciStatusParms.dwItem=MCI_STATUS_POSITION;
mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);
dCurrentPosition=mciStatusParms.dwReturn;
if(dCurrentPosition<=(m_dLength/16))
{
mciSendCommand(m_wDeviceID,MCI_SEEK,MCI_SEEK_TO_START,NULL);
mciSendCommand(m_wDeviceID,MCI_PLAY,0,(DWORD)(LPVOID)&mciPlayParms);
}
else
{
mciPlayParms.dwFrom=dCurrentPosition-(DWORD)(m_dLength/16);
mciSendCommand(m_wDeviceID,MCI_PLAY,MCI_FROM,(DWORD)(LPVOID)&mciPlayParms);
}
}
void CMIDIPlayDlg::OnNext()
{
// TODO: Add your control notification handler code here
isPause=true;
MCI_STATUS_PARMS mciStatusParms;
MCI_PLAY_PARMS mciPlayParms;
mciStatusParms.dwItem=MCI_STATUS_POSITION;
mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);
dCurrentPosition=mciStatusParms.dwReturn;
if((m_dLength-dCurrentPosition)<=(m_dLength/16))
{
mciSendCommand(m_wDeviceID,MCI_SEEK,MCI_SEEK_TO_END,NULL);
mciSendCommand(m_wDeviceID,MCI_PLAY,0,(DWORD)(LPVOID)&mciPlayParms);
}
else
{
mciPlayParms.dwFrom=dCurrentPosition+(DWORD)(m_dLength/16);
mciSendCommand(m_wDeviceID,MCI_PLAY,MCI_FROM,(DWORD)(LPVOID)&mciPlayParms);
}
} 4、加入Timer定时函数
void CMIDIPlayDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
MCI_STATUS_PARMS mciStatusParms;
mciStatusParms.dwItem=MCI_STATUS_POSITION;
mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,
(DWORD)(LPVOID)&mciStatusParms);
dCurrentPosition=mciStatusParms.dwReturn;
m_position.SetPos(dCurrentPosition);
CDialog::OnTimer(nIDEvent);
}
5、在StdAfx.h文件中添加
#include
#pragma comment (lib,"winmm.lib")
这样一个简单的MIDI/WAV播放器就做好了