mciSendString相关使用说明【转】

Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

mciSendString是用来播放多媒体文件的API指令,可以播放MPEG,AVI,WAV,MP3,等等,下面介绍一
下它的使用方法:

  该函数有四个参数:
第一个参数:要发送的命令字符串。字符串结构是:[命令][设备别名][命令参数].
第二个参数:返回信息的缓冲区,为一指定了大小的字符串变量.
第三个参数:缓冲区的大小,就是字符变量的长度.
第四个参数:回调方式,一般设为零
返回值:函数执行成功返回零,否则返回错误代码


一、常用命令
  1.打开(Open),格式:Open 设备名 [type 设备型式][alias 别名]
Dim mName as string
mName = "f:\\mpeg\\mpeg1.avi"
mciSendString "open mName type MPEGVideo Alias movie parent %u Style %u notify",0&, 0, 0
 其中:
open 操作命令
mName 全路径文件名
type MPEGVideo 是指打开MPEG,AVI等类型,如果不加这一句,就是打开WAV,MP3等
Alias movie 定义了该操作的别名为movie,后续操作只要指明别名即可
parent %u 源
Style %u 风格、样式
notify 通知

  2.播放(Play),格式:Play 设备名 [from 起点][to 终点]
mciSendString "play movie", 0&, 0, 0
mciSendString "play movie fullscreen", 0&, 0, 0 '全屏播放

  3.暂停(Pause):
mciSendString "pause movie", 0&, 0, 0

  4.继续(Resume):
mciSendString "resume movie", 0&, 0, 0

  5.停止(Stop):
mciSendString "stop movie", 0&, 0, 0

  6.关闭(Colse):
mciSendString "close movie", 0&, 0, 0

  7.前进到下一个位置:
mciSendString "step movie", 0&, 0, 0

  8.后退到上一个位置:
mciSendString "step movie reverse", 0&, 0, 0

  9.前进或后退 N 個位置(其中 N<0 即表示后退)
mciSendString "step movie by " & str(N), 0&, 0, 0

  10.获取当前播放位置:
Dim ST As String*64
mciSendString "status movie position", st, len(st), 0

  11. 获取媒体的总长度:
mciSendString "status movie length", st, len(st), 0
l=val(st) 'L就是所播放文件的长度

  12.获取播放信息:
Dim ST As String*64
mciSendString "status movie mode", ST, Len(ST), 0
If Left(ST, 7) = "stopped" Then (处理代码) '播放完毕

  13.循环播放:
mciSendString "play movie repeat", 0&, 0, 0


二、控制声音大小:
Dim V As Long
mciSendString "status movie volume", V, 0, 0 'V是获取的音量大小值。
V = 50
mciSendString "setaudio movie volume to " & V, &0, 0, 0 'V是设置的音量值


三、设置播放位置.(需事先设定时间格式),格式:Seek 设备名 [to 位置 | to start | to end]
Dim P1 as Long, P2 as Long
P1 = 100: P2 = 3000
mciSendString "seek movie to ", P1, 0, 0 'P1是当前起始位置,单位:毫秒
mciSendString "seek movie to start", 0&, 0, 0 '定位到开头位置
mciSendString "play movie", 0&, 0, 0 '定位后再播放
或者:
mciSendString "play movie FROM P1 to P2",0&, 0, 0 'P1是起始位置,P2是停止位置。单位:毫秒
mciSendString "seek movie to end", 0&, 0, 0 '定位到最后位置


四、在指定控件上播放视频:
mciSendString "open AVI 文件名 parent hWnd style child", 0&, 0, 0
其中,hWnd 是控件的句柄
执行上述命令之后,影片会被放置在控件的左上角,且影片的大小不受控件大小的影响,如果想要改变
影片播放的位置及大小,可以在執行 play 指令前先执行 put 指令,格式如下:
  mciendString "put AVI 文件名 window at X Y [Width Height]", 0&, 0, 0
其中:X、Y为影片左上角坐标,Width、Height为影片的宽高度


五、如果播放视频还可控制亮度
Dim B As Long
mciSendString "status movie brightness", B, 0, 0 'B是获取的亮度值。
B = 50
mciSendString "setvideo movie brightness to " & B, &0, 0, 0 'B是设置的亮度值


六、录音设置:
  录音前,用以下语句初始化
  1.设为8位:
mciSendString "set wave bitpersample 8", "", 0, 0
  2.设为11025Hz
mciSendString "set wave samplespersec 11025", "", 0, 0
  3.设为立体声:
mciSendString "set wave channels 2", "", 0, 0
  4.实现PCM格式(不一定正确):
MCISENDSTRING "set wave format tag pcm","", 0, 0
  5.开始录音:
mciSendString "close movie",0&,0,0
mciSendString "open new type WAVEAudio alias movie",0&,0,0
mciSendString "record movie",0&,0,0
  6.保存录音到c:\123.wav
mciSendString "stop movie",0&,0,0
mciSendString "save movie C:\\123.wav",0&,0,0
mciSendString "close movie",0&,0,0



此帖尚未评分
作者:一江秋水
专家分:4800



会员信息
发短消息
所属BLOG 发表时间:2008-12-9 17:07:00 [回复] [引用]
4 楼
七、开关光驱:
mciSendString "set cdaudio door open", "", 0, 0 '打开
mciSendString "set cdaudio door close", "", 0, 0 '关闭


八、其它
  1.设置设备的各种状态(Set)
Set alias_name[audio all off][audio all on][time format ms]:
  Set命令用来设置设备的各种状态.如:静音,有声音,时间格式为毫秒等.

  2.取得设备的状态(Status)
Status alias_name[length][mode][position]:
  Status命令用来取得设备的状态.如:该媒体文件的长度,该媒体文件所处状态,该媒体文件的当前位置等.
由于篇幅有限这里就不再说更多的命令集了.有兴趣的朋友可到我的网址详细的查询.

程式范例:

一、播放mp3、wav、mid等音乐

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Private Sub Form_Load()
Dim musicName As String
musicName = "(全路径音乐文件名)" '注意:路径和文件名决不能有空格
mciSendString "open " & musicName & " alias mymusic", 0&, 0, 0
End Sub

Private Sub Command1_Click() '开始播放
mciSendString "play mymusic", "", 0, 0
End Sub

Private Sub Command2_Click() '暂停
mciSendString "pause mymusic", "", 0, 0
End Sub

Private Sub Command3_Click() '继续
mciSendString "resume mymusic", 0&, 0, 0
End Sub

Private Sub Command4_Click() '停止
mciSendString "stop mymusic", 0&, 0, 0
End Sub

Private Sub Command5_Click() '关闭
mciSendString "close mymusic", 0&, 0, 0
End Sub


二、播放其它
Sub PlaySound(filename)
Dim cmd As String
Call mciSendString("close MyWav", 0, 0, 0) ' MyWav 这个名称可依需要来修改
cmd = "open " & filename & " type WAVEAudio alias MyWav"
'去掉 上面中的 type WAVEAudio, 则该副程式便可用来播放 midi 音乐档。
Call mciSendString(cmd, 0, 0, 0)
Call mciSendString("play MyWav", 0, 0, 0)
End Sub

' 假设要拨放 "c:\windows\Tada.wav"
PlaySound "c:\windows\Tada.wav"

播放 .flc 动画档案的方法与 .wav 及 .midi 并没有什麽不同,最後完成的程式大致如下:

Sub PlaySound(filename)
Dim cmd As String
Call mciSendString("close MyFlc", 0, 0, 0) ' MyFlc 这个名称可依需要来修改
cmd = "open " & filename & " alias MyFlc"
Call mciSendString(cmd, 0, 0, 0)
Call mciSendString("play MyFlc", 0, 0, 0)
End Sub

注:有些机器可能会因为没有安装 .flc 的驱动程式而无法播放,欲检查机器中是否安装有
播放 .flc 的驱动程式, 可先利用 Windows 提供的媒体播放程式播放 .flc 档案看看。

假设我们想将 .flc 放在 Picture1 之中播放, 则必须将 open MCI 指令由原先的:

"open " & filename & " alias MyFlc" 改成:
"open " & filename & " alias MyFlc parent " & Picture1.hWnd & " style " & WS_CHILD

则 .flc 就会在 Picture1 之中播放。

注:WS_CHILD = &H40000000

十、有关播放CD的格式:
&acute;设定时间格式为 Tracks(磁轨), 单位是:毫秒
lRet = mciSendString("set cd time format tmsf", 0&, 0, 0)
&acute;从头开始播放
lRet = mciSendString("play cd", 0&, 0, 0)
&acute;也可以指定从第几首歌 (Track) 开始播放,例如以下指定从第 3 首歌开始播放
&acute;nCurrentTrack = 3
lRet = mciSendString("play cd from" & Str(nCurrentTrack), 0&, 0, 0)


下面用了一个线程进行播放系统的声音,主线程进行控制:现在没有用CreateWindow

来创建窗口,而是用CreateDialog,子窗口时,也是直接用hwndEdit=GetDlgItem()实现的

#include <windows.h>
#include <mmsystem.h>
#include <process.h>
#include "resource.h"
#pragma comment(lib,"winmm.lib")

void PrintfText(HWND hwndEdit,TCHAR *szFile);
void Thread(PVOID pvoid);
BOOL CALLBACK About(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
typedef struct
{
HWND hwnd;
BOOL bkill;
} PARAMS;

INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
HWND hwnd;
MSG msg;
hwnd=CreateDialog(hInstance,
MAKEINTRESOURCE(IDD_DIALOG1),
NULL,
(DLGPROC)About);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while(GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}

BOOL CALLBACK About(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static HWND hwndEdit;
DWORD dwExitCode;
static PARAMS Params;
static HANDLE handle;
TCHAR szReturn[256];
switch(msg)
{
case WM_INITDIALOG:
hwndEdit=GetDlgItem(hwnd,IDC_EDIT1);
Params.hwnd=hwndEdit;
Params.bkill=FALSE;
return TRUE;
case WM_SIZE:
if (SIZE_MINIMIZED!=wParam)
MoveWindow(hwndEdit,
0,
0,
LOWORD(lParam),
HIWORD(lParam),
TRUE);
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_FILE_START:
Params.hwnd=hwndEdit;
Params.bkill=FALSE;
handle=(HANDLE)_beginthread (Thread,0,(PVOID)&Params) ;
return TRUE;
case IDM_FILE_STOP:
mciSendString(TEXT("open cdaudio"),NULL,0,hwnd);
mciSendString(TEXT("Set cdAudio door open wait"),NULL,0,hwnd);
mciSendString(TEXT("Set cdAudio door closed wait"),NULL,0,hwnd);
mciSendString(TEXT("close cdaudio"),NULL,0,hwnd);
Params.bkill=TRUE;
CloseHandle(handle);
return TRUE;
case IDM_FILE_EXIT:
SendMessage(hwnd,WM_CLOSE,0,0);
return TRUE;
}
break;
case WM_CLOSE:
CloseHandle(handle);
GetExitCodeProcess(GetCurrentProcess(),&dwExitCode);
PostMessage(hwnd,WM_QUIT,(WPARAM)wParam,0);
EndDialog(hwnd,TRUE);
return TRUE;
}
return FALSE;
}

void Thread(PVOID pvoid)
{
HANDLE handle;
WIN32_FIND_DATA wfd;
TCHAR szBuf[MAX_PATH];
volatile PARAMS *pParams;
pParams=(PARAMS *)pvoid;
while(!pParams->bkill)
{
if (!pParams->bkill)
handle=FindFirstFile(TEXT("C:\\WINDOWS\\Media\\*"),&wfd);
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && lstrcmp(wfd.cFileName,TEXT(".")) && lstrcmp(wfd.cFileName,TEXT("..")))
{
ZeroMemory(szBuf,sizeof(szBuf));
wsprintf(szBuf,TEXT("C:\\WINDOWS\\Media\\%s\r\n"),wfd.cFileName);
PrintfText(pParams->hwnd,szBuf);
wsprintf(szBuf,TEXT("C:\\WINDOWS\\Media\\%s"),wfd.cFileName);
PlaySound(szBuf,NULL,SND_FILENAME|SND_SYNC);
}
while(!pParams->bkill && FindNextFile(handle,&wfd))
{
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && lstrcmp(wfd.cFileName,TEXT(".")) && lstrcmp(wfd.cFileName,TEXT("..")))
{
ZeroMemory(szBuf,sizeof(szBuf));
wsprintf(szBuf,TEXT("C:\\WINDOWS\\Media\\%s\r\n"),wfd.cFileName);
PrintfText(pParams->hwnd,szBuf);
wsprintf(szBuf,TEXT("C:\\WINDOWS\\Media\\%s"),wfd.cFileName);
PlaySound(szBuf,NULL,SND_FILENAME|SND_SYNC);
}
}
}
_endthread();
}

void PrintfText(HWND hwndEdit,TCHAR *szFile)
{
SendMessage(hwndEdit,EM_SETSEL,(WPARAM)-1,(LPARAM)-1);
SendMessage(hwndEdit,EM_REPLACESEL,FALSE,(LPARAM)szFile);
SendMessage(hwndEdit,EM_SCROLLCARET,0,0);
}

你可能感兴趣的:(windows,String,command,cmd,null,callback)