在註冊表HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\MCI Extensions中有所有的文件類型,和相對應的播放設備。
MCI播放mp3的例子:
- #include<windows.h>
- #include<stdio.h>
- #include<mmsystem.h>
- #pragma comment(lib,"winmm.lib")
- void main()
- {
- char buf[128];
- //use mciSendString()
- //mciSendString("play e:\\songs\\把根留住.mp3",buf,sizeof(buf),NULL);
- //mciSendString("play e:\\songs\\zhj.mp3",buf,sizeof(buf),NULL);
- char str[128] = {0 };
- int i = 0;
- //use mciSendCommand
- MCI_OPEN_PARMS mciOpen;
- MCIERROR mciError;
- SetWindowText(NULL,"12345");
- //mciOpen.lpstrDeviceType = (LPCTSTR)MCI_ALL_DEVICE_ID;
- //mciOpen.lpstrDeviceType = "waveaudio"; //只能播放.wav文件
- //mciOpen.lpstrDeviceType = "avivideo"; //*.avi
- mciOpen.lpstrDeviceType = "mpegvideo";
- //mciOpen.lpstrDeviceType = "sequencer";
- mciOpen.lpstrElementName = "e:\\songs\\zhj.mp3";
- //mciOpen.lpstrElementName = "e:\\movie\\first.avi";
- //mciOpen.lpstrElementName = "c:\\winnt\\media\\Windows 登錄音.wav";
- mciError = mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD)&mciOpen);
- if(mciError)
- {
- mciGetErrorString(mciError,buf,128);
- printf("send MCI_OPEN command failed:%s\n",buf);
- return;
- }
- 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");
- return;
- }
- /*
- while(1)
- {
- sprintf(str,"now playing\t%d\tseconds",i);
- printf("%s\r",str);
- i++;
- Sleep(1000);
- }
- */
- }