#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;
}
//'====================================================
//'获得当前媒体的状态是不是在播放
//'====================================================
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);
}
本文地址:http://com.8s8s.com/it/it2533.htm