一般播放MP3常见的有两种方法,一种是自己解码,另外一种用系统的库,比如MCI,当然如果可以用控件直接用个控件会更方便。下面写的是一个MCI播放MP3的例子:
#include
#include
#include
#include
#include
#pragma comment(lib,”winmm.lib”)
void main()
{
char str[128]={0};
int i = 0;
char buf[128]={0};
MCI_OPEN_PARMS mciOpen;
MCIERROR mciError;
mciOpen.lpstrDeviceType = “mpegvideo”;
mciOpen.lpstrElementName = “c:\1.mp3”;
mciError = mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD)&mciOpen);
if(mciError)
{
mciGetErrorString(mciError,buf,128);
printf(“%sn”,buf);
goto Error;
}
UINT DeviceID = mciOpen.wDeviceID ;
MCI_PLAY_PARMS mciPlay;
mciError = mciSendCommand(DeviceID,MCI_PLAY,0 ,(DWORD)&mciPlay);
if(mciError)
{
printf(“send MCI_PLAY command failedn”);
goto Error;
}
//WinExec(“sndvol32.exe”,SW_SHOWNORMAL);
//这个可以打开音量控制不过可以用编程实现。
while(1)
{
sprintf(str,”播放时间:t%d”,i);
printf(“%sr”,str);
i++;
Sleep(1000);
}
Error:
system(“pause”);
}