//ÓÃMCIÃüÁîÀ´ÊµÏÖ¶àýÌåµÄ²¥·Å¹¦ÄÜ //ÏÂÃæµÄÄÚÈݼ¸ºõÓв¥·ÅÆ÷Èí¼þµÄ¸÷ÖÖ¹¦ÄÜ£¬ÄãÖ»ÊÇÒýÓÃÕâЩº¯Êý¾ÍÄÜ×ö³öÒ»¸ö²¥·ÅÆ÷À´ #include < MMSystem.h > #include < Vfw.h > #include < shlobj.H > #pragma comment(lib, "winmm.lib") #pragma comment(lib, "Vfw32.lib") #define AudioStereo 0 #define AudioLeft 1 #define AudioRight 2 WNDPROC OldProc; LPSTR GetDriverID(LPSTR ff); bool OpenMusic(LPSTR FileName,HWND hWnd); bool PlayMusic(); long GetMusicLength(); bool PutToWindow(RECT rc); long GetMusicPos(); bool SetMusicPos(long Position); bool PauseMusic(); bool CloseMusic(); bool SetAudioSource(int AudioSource); bool PlayFullScreen(); bool SetVolume(int Volume); bool SetSpeed(int Speed); bool SetAudioOnOff(bool AudioOff); bool SetWindowShow(bool WindowOff); bool IsPlaying(); HWND GetWindowHandle(); MCIDEVICEID GetDeviceID(); LRESULT CALLBACK MCIWindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); bool ShowOpenSave(LPSTR FileName,bool OpenOrSave,HWND hWnd); bool SavePicture(LPSTR FileName,HWND hWnd); /*'======================================================*\ '¸ù¾ÝÎļþÃû£¬È·¶¨É豸 \'=======================================================*/ LPSTR GetDriverID(LPSTR ff) { char aa[3]; int i; char bb[3]; int lenff=strlen(ff); for(i=0;i<3;i++){ aa[i]=ff[lenff-3+i]; if(aa[i]>=97||aa[i]<=122)aa[i]-=32; } aa[3]=0; strcpy(bb,"MID"); if(strcmp(aa,bb)==0)return "Sequencer"; strcpy(bb,"RMI"); if(strcmp(aa,bb)==0)return "Sequencer"; strcpy(bb,"IDI"); if(strcmp(aa,bb)==0)return "Sequencer"; strcpy(bb,"WAV"); if(strcmp(aa,bb)==0)return "Waveaudio"; return "MPEGVideo"; } //======================================================= //´ò¿ªMCIÉ豸£¬FILENAMEΪÎļþÃû,´«Öµ´ú±í³É¹¦Óë·ñ //======================================================= bool OpenMusic(LPSTR FileName,HWND hWnd=NULL) { CloseMusic(); CString ShortPathName; CString tmpStr=FileName; if(tmpStr.Left(7)=="http://") ShortPathName=FileName; else GetShortPathName(FileName, ShortPathName.GetBuffer(0), 255); // MessageBox(NULL,ShortPathName.GetBuffer(0),"",MB_OK); char *DrvID=GetDriverID(ShortPathName.GetBuffer(0)); CString lpstrCommand; lpstrCommand.Format("open %s type %s alias NOWMUSIC", ShortPathName.GetBuffer(0), DrvID); if(strcmp(DrvID,"MPEGVideo")>=0) { if(hWnd!=NULL) lpstrCommand.Format("open %s type %s alias NOWMUSIC parent %d style child", ShortPathName.GetBuffer(0), DrvID,(int)hWnd); else lpstrCommand+=" style overlapped ";} if(mciSendString(lpstrCommand.GetBuffer(0),NULL,0,0)==0) { OldProc=(WNDPROC)GetWindowLong(GetWindowHandle(),GWL_WNDPROC); SetWindowLong(GetWindowHandle(),GWL_WNDPROC,(long)MCIWindowProc); return true; } return false; } //======================================================= //¹Ø±ÕMCIÉ豸£¬FILENAMEΪÎļþÃû,´«Öµ´ú±í³É¹¦Óë·ñ //======================================================= bool CloseMusic() { if(mciSendString("close NOWMUSIC", NULL, 0, 0)==0) return true; else return false; } //'====================================================== //'²¥·ÅÎļþ //'====================================================== bool PlayMusic() { if(mciSendString("play NOWMUSIC", NULL, 0, 0)==0) return true; else return false; } //'====================================================== //'»ñȡýÌåµÄ³¤¶È //'====================================================== long GetMusicLength() { char RefStr[80]; mciSendString("status NOWMUSIC length", RefStr, 80, 0); return atol(RefStr); } //'====================================================== //'Ìí³ä»Ãæ //'====================================================== bool PutToWindow(RECT rc) { CString lpstrCommand; lpstrCommand.Format("put NOWMUSIC window at %d %d %d %d", rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top); if(mciSendString(lpstrCommand.GetBuffer(0),NULL,0,0)==0) return true; else return false; } //'====================================================== //'»ñÈ¡µ±Ç°²¥·Å½ø¶È //'====================================================== long GetMusicPos() { char RefStr[80]; mciSendString("status NOWMUSIC position", RefStr, 80, 0); return atol(RefStr); } //'====================================================== //'ÉèÖÃýÌåµÄµ±Ç°½ø¶È //'====================================================== bool SetMusicPos(long Position) { CString lpstrCommand; lpstrCommand.Format("seek NOWMUSIC to %d", Position); if(mciSendString(lpstrCommand.GetBuffer(0),NULL,0,0)==0) return true; else return false; } //'====================================================== //'ÔÝÍ£²¥·Å //'====================================================== bool PauseMusic() { if(mciSendString("pause NOWMUSIC", NULL, 0, 0)==0) return true; else return false; } //'====================================================== //'ÉèÖÃÉùµÀ //'====================================================== bool SetAudioSource(int AudioSource) { CString lpstrCommand; lpstrCommand="setaudio NOWMUSIC source to "; switch(AudioSource){ case AudioStereo: lpstrCommand+="stereo"; break; case AudioLeft: lpstrCommand+="left"; break; case AudioRight: lpstrCommand+="right"; break; } if(mciSendString(lpstrCommand.GetBuffer(0),NULL,0,0)==0) return true; else return false; } //'====================================================== //'È«ÆÁ²¥·Å //'====================================================== bool PlayFullScreen() { if(mciSendString("play NOWMUSIC fullscreen", NULL, 0, 0)==0) return true; else return false; } //'===================================================== //'ÉèÖÃÉùÒô´óС1-1000 //'===================================================== bool SetVolume(int Volume) { CString lpstrCommand; lpstrCommand.Format("setaudio NOWMUSIC volume to %d", Volume); if(mciSendString(lpstrCommand.GetBuffer(0),NULL,0,0)==0) return true; else return false; } //'===================================================== //'ÉèÖò¥·ÅËÙ¶È1-2000 //'===================================================== bool SetSpeed(int Speed) { CString lpstrCommand; lpstrCommand.Format("set NOWMUSIC speed %d", Speed); if(mciSendString(lpstrCommand.GetBuffer(0),NULL,0,0)==0) return true; else return false; } //'==================================================== //'¾²ÒôTrueΪ¾²Òô£¬FALSEΪȡÏû¾²Òô //'==================================================== bool SetAudioOnOff(bool AudioOff) { if(AudioOff){ if(mciSendString("setaudio NOWMUSIC off",NULL,0,0)==0) return true; } else{ if(mciSendString("setaudio NOWMUSIC on",NULL,0,0)==0) return true; } return false; } //'==================================================== //'ÊÇ·ñÓлÃæTrueΪÓУ¬FALSEΪȡÏû //'==================================================== bool SetWindowShow(bool WindowOff) { if(WindowOff){ if(mciSendString("window NOWMUSIC state show",NULL,0,0)==0) return true; } else{ if(mciSendString("window NOWMUSIC state hide",NULL,0,0)==0) return true; } return false; } //'==================================================== //'»ñµÃµ±Ç°Ã½ÌåµÄ״̬ÊDz»ÊÇÔÚ²¥·Å //'==================================================== bool IsPlaying() { // char RefStr[10]; CString RefStr; if(mciSendString("status NOWMUSIC mode", RefStr.GetBuffer(10), 10, 0)==0) { if(RefStr.Find("playing")>=0|| RefStr.Find("²¥·Å")>=0) return true; } return false; } //'==================================================== //'»ñµÃ²¥·Å´°¿ÚµÄhandle //'==================================================== HWND GetWindowHandle() { char RefStr[80]; mciSendString("status NOWMUSIC window handle", RefStr, 80, 0); return (HWND)atol(RefStr); } //'==================================================== //'»ñÈ¡DeviceID //'==================================================== MCIDEVICEID GetDeviceID() { return mciGetDeviceID("NOWMUSIC"); } //'==================================================== //'´¦Àí´°¿Úʼþ //'==================================================== LRESULT CALLBACK MCIWindowProc(HWND hWnd,UINT message, WPARAM wParam,LPARAM lParam) { // HDC hdc; int wmId; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); //MessageBox(NULL,"OK","sd",MB_OK); /*switch(wmId) { default: break; }//*/ break; case WM_LBUTTONUP: return 0; break; case WM_RBUTTONUP: POINT pt; GetCursorPos(&pt); break; case WM_PAINT: break; default: break; } return CallWindowProc(OldProc,hWnd, message,wParam,lParam); } |