学习MCI时看别人样例手敲代码出现的一个很经典的错误。
在C语言中定义的变量没有放在函数的开头。
#include <string.h>
#include <windows.h>
#include <Digitalv.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
char buf[128];
MCI_OPEN_PARMS mciOpen;
MCIERROR mciError;
UINT DeviceID;
MCI_PLAY_PARMS mciPlay;
mciOpen.lpstrDeviceType = L"mpegvideo";
mciOpen.lpstrElementName = L"year.mp3";
mciError = mciSendCommand(0, MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mciOpen);
if(mciError)
{
mciGetErrorString(mciError, (LPWSTR)buf, 128);
MessageBox(NULL, TEXT("send MCI_OPEN command failed"), TEXT("ERROR"), 0);
return;
}
DeviceID = mciOpen.wDeviceID;
mciError = mciSendCommand(DeviceID, MCI_PLAY, MCI_WAIT | MCI_DGV_PLAY_REPEAT, (DWORD)&mciPlay);
if(mciError)
{
mciGetErrorString(mciError, (LPWSTR)buf, 128);
MessageBox(NULL, TEXT("send MCI_PLAY command failed"), TEXT("ERROR"), 0);
return;
}
return 0;
}