#include <windows.h> #include <stdio.h> #include <mmsystem.h> #include <shellapi.h> #include <AFXCOM_.H> #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("%s/n",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 failed/n"); goto Error; } //WinExec("sndvol32.exe",SW_SHOWNORMAL); //这个可以打开音量控制不过可以用编程实现。 while(1) { sprintf(str,"播放时间:/t%d",i); printf("%s/r",str); i++; Sleep(1000); } Error: system("pause"); }
MCI_OPEN_PARMS mciOpen;
MCIERROR mciError;
MCI_STATUS_PARMS mciStatusParms;
WORD m_wDeviceID;
这里只介绍mciSendCommand命令
0.打开设备
mciError = mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD)&mciOpen);
在打开设备后,就获取了设备的ID,后面的所有操作,都必须要使用这个ID号,即:
UINT DeviceID = mciOpen.wDeviceID ;
1.播放
MCI_PLAY_PARMS mciPlay; mciOpen.lpstrDeviceType = "mpegvideo"; mciOpen.lpstrElementName =fileName.GetString(); mciError = mciSendCommand(m_wDeviceID,MCI_PLAY,0 ,(DWORD)&mciPlay);//这里的 m_wDeviceID 是在 打开设备时,获取的设备ID
2.暂停
mciSendCommand(mciOpen.wDeviceID,MCI_PAUSE,NULL ,NULL);
3. 停止
mciSendCommand(mciOpen.wDeviceID,MCI_STOP,NULL ,NULL);
mciSendCommand(mciOpen.wDeviceID,MCI_SEEK,MCI_SEEK_TO_START ,NULL);
4.获得播放进度
mciStatusParms.dwItem=MCI_STATUS_POSITION; mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_WAIT|MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);//关键,取得位置 int m_dPosition=mciStatusParms.dwReturn;
5.获得整个音频的时间长度
UINT DeviceID = mciOpen.wDeviceID ; mciStatusParms.dwItem=MCI_STATUS_LENGTH; mciSendCommand(DeviceID,MCI_STATUS,MCI_WAIT|MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);//关键,取得长度 m_dLength=mciStatusParms.dwReturn;
6.在指定位置播放
MCI_PLAY_PARMS mciPlay; mciPlay.dwFrom=m_dPosition; mciError = mciSendCommand(m_wDeviceID,MCI_PLAY,MCI_FROM,(DWORD)&mciPlay);
7.控制音量
MCI_DGV_SETAUDIO_PARMS mciSetAudioPara; mciSetAudioPara.dwItem = MCI_DGV_SETAUDIO_VOLUME; mciSetAudioPara.dwValue = atoi(m_Volume.GetBuffer(m_Volume.GetLength())); //here is the value of the Volume(0--1000) mciSendCommand(m_wDeviceID, MCI_SETAUDIO, MCI_DGV_SETAUDIO_VALUE | MCI_DGV_SETAUDIO_ITEM, (DWORD)(LPVOID)&mciSetAudioPara);