C语⾔micstring函数,micSendString函数使⽤⽅法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",
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),
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
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 Longc++中string的用法
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
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,
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论