【vc】【多媒体编程应用】mciSendString的使用
2007-06-03 16:05
369 查看
mciSendString是用来播放多媒体文件的API指令,可以播放MPEG,AVI,WAV,MP3,等等,下面我们来介绍一
下它的使用方法:
一,打开多媒体文件。
首先在Dialog上面放一个Picture控件,MCISendString就在这个Picture控件的平面上播放多媒体文件,
设Picture控件的ID为IDC_STATIC1:
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC1);
HWND h=pStatic->GetSafeHwnd();
CString open1;
char buf[256];
open1.Format("open f://mpeg//mpeg1.avi type MPEGVideo Alias movie parent %u Style %u notify", h,WS_CHILD);
mciSendString(open1.GetBuffer(open1.GetLength()),buf,sizeof(buf),NULL);
这样F盘下面的mpeg目录下的mpeg1.avi就打开了,其中的type MPEGVideo是指打开MPEG,AVI等类型,如果不加
type MPEGVideo这一句,就是打开WAV,MP3等,Alias movie定义了mpeg1.avi的别名为movie,以后可以通过操
做movie就可以操作mpeg1.avi。
二,播放多媒体文件。
上面我们已经打开了f:/mpeg/mpeg1.avi,现在我们来播放它:
mciSendString("play movie",buf,sizeof(buf),NULL);
如果想全屏播放:
mciSendString("play movie fullscreen",buf,sizeof(buf),NULL);
三,暂停播放。
mciSendString("pause movie",buf,sizeof(buf),NULL);
四,停止播放。
mciSendString("close movie",buf,sizeof(buf),NULL);
五,得到所播放文件的长度。
char sPosition[256];long lLength;
ciSendString("Status movie length", sPosition, 255,0);lLength=strtol(sPosition, NULL, 10);
其中lLength就是所播放文件的长度。
六,得到所播放文件的声音大小。
char chVolume[256];long lVolume;mciSendString("status movie volume",chVolume,255,0);lVolume=strtol(chVolume,NULL,10);其中lVolume就是所播放文件的声音大小。
七,到你指定的位置播放。
CString step1;long lPosition;
lPosition=100;
step1.Format("seek movie to %ld",lPosition);
mciSendString(step1.GetBuffer(step1.GetLength()),buf,sizeof(buf),0);
其中lPosition就是要到的播放位置,可以由你指定。
==========================================================
下附一个播放器的程序
#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);
}
下它的使用方法:
一,打开多媒体文件。
首先在Dialog上面放一个Picture控件,MCISendString就在这个Picture控件的平面上播放多媒体文件,
设Picture控件的ID为IDC_STATIC1:
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC1);
HWND h=pStatic->GetSafeHwnd();
CString open1;
char buf[256];
open1.Format("open f://mpeg//mpeg1.avi type MPEGVideo Alias movie parent %u Style %u notify", h,WS_CHILD);
mciSendString(open1.GetBuffer(open1.GetLength()),buf,sizeof(buf),NULL);
这样F盘下面的mpeg目录下的mpeg1.avi就打开了,其中的type MPEGVideo是指打开MPEG,AVI等类型,如果不加
type MPEGVideo这一句,就是打开WAV,MP3等,Alias movie定义了mpeg1.avi的别名为movie,以后可以通过操
做movie就可以操作mpeg1.avi。
二,播放多媒体文件。
上面我们已经打开了f:/mpeg/mpeg1.avi,现在我们来播放它:
mciSendString("play movie",buf,sizeof(buf),NULL);
如果想全屏播放:
mciSendString("play movie fullscreen",buf,sizeof(buf),NULL);
三,暂停播放。
mciSendString("pause movie",buf,sizeof(buf),NULL);
四,停止播放。
mciSendString("close movie",buf,sizeof(buf),NULL);
五,得到所播放文件的长度。
char sPosition[256];long lLength;
ciSendString("Status movie length", sPosition, 255,0);lLength=strtol(sPosition, NULL, 10);
其中lLength就是所播放文件的长度。
六,得到所播放文件的声音大小。
char chVolume[256];long lVolume;mciSendString("status movie volume",chVolume,255,0);lVolume=strtol(chVolume,NULL,10);其中lVolume就是所播放文件的声音大小。
七,到你指定的位置播放。
CString step1;long lPosition;
lPosition=100;
step1.Format("seek movie to %ld",lPosition);
mciSendString(step1.GetBuffer(step1.GetLength()),buf,sizeof(buf),0);
其中lPosition就是要到的播放位置,可以由你指定。
==========================================================
下附一个播放器的程序
//用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;
}
//'====================================================
//'获得当前媒体的状态是不是在播放
//'====================================================
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);
}
相关文章推荐
- 【vc】【多媒体编程应用】MCI编程之 mciSendCommand 和mciSendString
- 【vc】【多媒体编程应用】MCI编程之 mciSendCommand
- ActiveDAQ控件的VC编程 数据采集 VARIANT数据类型的使用
- VC++界面编程之--使用分层窗口实现界面皮肤
- 《数据结构》实验一:VC编程环境灵活应用
- 【Android 多媒体应用】使用MediaCodec解码使用AudioTrack播放音频数据
- 《数据结构》实验一: VC编程工具的灵活使用
- Windows phone 7 高级编程——使用visual stdio 、silverlight与XNA进行应用和游戏开发
- "《数据结构》实验一:VC编程工具的灵活使用“以及总结
- VC++ MFC 编程中 IP 控件的使用
- [ZT]VC多文档应用中OpenGL的使用
- 并行编程之跨平台使用SSE、AVX指令集心得——以单精度浮点数组求和为例(支持vc、gcc,兼容Windows、Linux、Mac)
- ActiveDAQ控件的VC编程 数据采集 VARIANT数据类型的使用
- 《数据结构》实验一: VC编程工具的灵活使用(实验报告)
- 《数据结构》实验一:VC编程环境灵活应用
- Symbian OS 图形应用编程 | RWsSprite 精灵的使用介绍(转)
- 《数据结构》实验一: VC编程工具的灵活使用
- 《数据结构》实验1.1:VC编程环境灵活应用
- 《数据结构》实验1.3:VC编程环境灵活应用
- 《数据结构》实验一: VC编程工具的灵活使用