#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播放MP3
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);