链接库和头文件:
winmm.lib,mmsystem.h
函数原型:
【Unicode版】
WINMMAPI MCIERROR WINAPI mciSendStringW(IN LPCWSTR lpstrCommand,
OUT LPWSTR lpstrReturnString, IN UINT uReturnLength, IN HWND
hwndCallback);
【Ansi版】
WINMMAPI MCIERROR WINAPI mciSendStringA(IN LPCSTR lpstrCommand, OUT
LPSTR lpstrReturnString, IN UINT uReturnLength, IN HWND
hwndCallback);
参数:
LPCSTR lpstrCommand:要发送的命令字符串。(字符串结构是:[命令][设备别名][命令参数])
OUT LPSTR lpstrReturnString:返回信息的缓冲区。(指定了大小的字符串变量)
IN UINT uReturnLength:缓冲区的大小,就是lpstrReturnString:的长度
IN HWND hwndCallback:回调方式,一般设为零
返回值:
返回一个MCIERROR错误代码,为0时说明执行成功,配合mciGetErrorString使用
一、常用命令
1.打开:
//播放cd
MCIERROR error;
TCHAR szReturn[1024];
TCHAR strmci[1024] = L"paly cdaudio";
error=mciSendString(strmci,szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
//在控件里播放影音文件
HWND hStatic = ::GetDlgItem(hDlg,IDC_STATIC_Play);
CString strOpen;
strOpen.Format(L"open f:\\movie.avi type MPEGVideo Alias movie
parent %u Style %u notify",hStatic,WS_CHILD);
//f:\movie.avi为视频文件
error =
mciSendString(strOpen,szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
2.播放:
mciSendString(L"play
movie",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
mciSendString(L"play movie
fullscreen",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
mciSendString(L"play
cdaudio",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);//CD-ROM中的第一首歌
3.暂停:
mciSendString(L"pause
movie",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);//视频
mciSendString(L"pause
cdaudio",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);//CD-ROM
4.继续:
mciSendString(L"resume
movie",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
mciSendString(L"resume
cdaudio",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
5.停止:
mciSendString(L"stop
movie",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
mciSendString(L"stop
cdaudio",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
6.关闭:
mciSendString(L"close
movie",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
mciSendString(L"close
cdaudio",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
7.前进到下一个位置:
mciSendString(L"step
movie",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
8.后退到上一个位置:
mciSendString(L"step movie
reverse",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
9.前进或后退 N 個位置(其中 N<0 表示后退)
CString strPlay=L"step movie by -1";
mciSendString(strPlay,szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
10.获取当前播放位置:
TCHAR st[1024];
mciSendString(L"status movie position",st,wcslen(st),0);
11. 获取媒体的总长度:
mciSendString(L"status movie length",st,wcslen(st),0);
12.获取播放信息:
CString st;
mciSendString(L"status movie mode",st,st.GetLeng(),0);
if(st="stopped")//播放完毕
13.循环播放:
mciSendString(L"play movie
repeat",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
二、控制声音大小:
mciSendString(L"status movie
volume",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);//获取的音量大小值
CString str;
str.Fomat(L"setaudio movie volume to %s",szReturn);
mciSendString(str,szReturn,sizeof(szReturn)/sizeof(TCHAR),0);//设置的音量值
三、控制亮度(如果是播放视频)
mciSendString(L"status movie
brightness",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);//获取的亮度值。
str.Fomat(L"setvideo movie brightness to %s",szReturn);
mciSendString(str,szReturn,sizeof(szReturn)/sizeof(TCHAR),0);//设置的亮度值
四、到指定的位置播放。
CString step;
long lPosition=100; //指定位置
step.Format(L"seek movie to %d",lPosition);
mciSendString(step,szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
//定位到开头位置后再播放
mciSendString(L"seek movie to
start",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
mciSendString(L"play
movie",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
//定位到最后位置
mciSendString(L"seek movie to
end",szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
或者:
long lend=500;
step.Format(L"play movie from %d to
%d",lPosition,lend);//从lPosition播放到lend
mciSendString(step,szReturn,sizeof(szReturn)/sizeof(TCHAR),0);
//以上单位为毫秒
五、录音设置:
录音前,用以下语句初始化
1.设为8位:
mciSendString(L"set wave bitpersample 8",NULL,NULL,0);
2.设为11025Hz
mciSendString(L"set wave samplespersec 11025",NULL,NULL,0);
3.设为立体声:
mciSendString(L"set wave channels 2",NULL,NULL,0);
4.实现PCM格式:
mciSendString(L"set wave format tag pcm",NULL,NULL,0);
5.开始录音:
mciSendString(L"close movie",NULL,NULL,0);
mciSendString(L"open new type WAVEAudio alias
movie",NULL,NULL,0);
mciSendString(L"record movie",NULL,NULL,0);
6.保存录音
mciSendString(L"stop movie",NULL,NULL,0);
mciSendString(L"save movie C:\\djer.wav",NULL,NULL,0);
mciSendString(L"close movie",NULL,NULL,0);
七、开关光驱:
mciSendString(L"set cdaudio door open",NULL,NULL,0);//打开
mciSendString(L"set cdaudio door close",NULL,NULL,0);//关闭