csdn上已经有好多朋友问过诸如:
“如何播放avi、wave、midi文件”、:
“谁知道用api播放avi,mpg的详细方法?要可以设定将图像放置到设定的窗体中”、
“如何同时播放两个wav文件”
的问题,
其实用一个类模块就一切搞定,不需要什么控件之类的东西
下面这个类模块,我研究后将它修改得更好用了
将下面这个类模块存为mmedia.cls
—————————————————-
option explicit
————–truezq 最新更新2001-01-12———————
文件名: mmedia.cls
说明: : 一个多媒体类,能播放avi、wave、midi文件
用法:
dim multimedia as new mmedia
multimedia.mmopen “c:\test.wav”
multimedia.mmplay
!记住:在程序结束时,一定要用set multimedia=nothing释放资源!!!
—————————————————–
-=-=-=- 属性 -=-=-=-
sfilename 当前的文件名
nlength 文件长度(只读)
nposition 当前位置
sstatus 当前状态(只读)
bwait true/false.决定是否等待播放完
-=-=-=- 方法 -=-=-=-=-
mmopen <filename> 打开要播放的文件
mmclose 关闭当前文件
mmpause 暂停
mmstop 停止 停止后可以跳到开始再次播放
mmseek <position> seeks to a position in the file
mmplay 播放
————————————————————–
private salias as string 别名
private hwnd as long
private sfilename as string 当前的文件名
private nlength as single 文件长度
private nposition as single 当前位置
private sstatus as string 当前状态
private bwait as boolean 决定是否等待播放完
const ws_child = &h40000000
———— api 声明 ————-
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 declare function getactivewindow lib “user32” () as integer
当sthefile是一个avi文件时,参数hwnd指定动画在哪里播放
若hwnd=0,则新开一个窗口播放动画。
如果听不到midi音乐,请在windows下用媒体播放器测试一下。
文件名不能带空格
public sub mmopen(byval sthefile as string, optional hwnd as long = 0)
dim nreturn as long
dim stype as string 文件类型
static nnum as integer
if salias <> “” then 关闭开始打开的文件
mmclose
end if
if (dir(sthefile) = “”) then 判断是否是一个存在的文件
sfilename = “文件” & sthefile & ” 不存在!”
exit sub
else
sfilename = sthefile
nnum = nnum + 1
end if
stop
salias = sfilename 用文件名作别名,避免别名冲突!
判断文件类型
select case ucase$(right$(sthefile, 3))
case “wav”
stype = “waveaudio”
case “avi”
stype = “avivideo”
case “mid”
stype = “sequencer”
case else
未知文件格式,退出。
exit sub
end select
if stype = “avivideo” and hwnd > 0 then
nreturn = mcisendstring(“open ” & sthefile & ” alias ” & salias _
& ” type avivideo parent ” & hwnd & ” style ” & ltrim$(str$(ws_child)), 0&, 0, 0)
else
nreturn = mcisendstring(“open ” & sthefile & ” alias ” & salias _
& ” type ” & stype, “”, 0, 0)
end if
end sub
关闭当前打开的多媒体文件
public sub mmclose()
dim nreturn as long
如果没有文件打开,则退出
if salias = “” then exit sub
nreturn = mcisendstring(“close ” & salias, “”, 0, 0)
salias = “”
sfilename = “”
end sub
暂停
public sub mmpause()
dim nreturn as long
if salias = “” then
exit sub
elseif status = “paused” then 如果先前已经暂停了,则解除暂停
mmplay
else
nreturn = mcisendstring(“pause ” & salias, “”, 0, 0)
end if
nposition = position
end sub
播放
public sub mmplay()
dim nreturn as long
if salias = “” then
exit sub
elseif position = length then 如果已经到末尾
mmseek 0 跳到开始处
end if
if bwait then
nreturn = mcisendstring(“play ” & salias & ” wait”, “”, 0, 0)
else
nreturn = mcisendstring(“play ” & salias, “”, 0, 0)
end if
end sub
停止
停止后跳到开始,以便再次播放
public sub mmstop()
dim nreturn as long
if salias = “” then exit sub
nreturn = mcisendstring(“stop ” & salias, “”, 0, 0)
mmseek 0 跳到开始位置
end sub
跳到指定的位置,并且处于暂停状态
当nposition的值>length 或者nposition<0时,将忽略这次操作
public sub mmseek(byval nposition as single)
dim nreturn as long
nreturn = mcisendstring(“seek ” & salias & ” to ” & nposition, “”, 0, 0)
end sub
方法filename返回当前打开的文件名
property get filename() as string
filename = sfilename
end property
指定要播放的文件名,然后将它打开
对于需要指定容器的avi文件,不要以这种方式打开。
property let filename(byval sthefile as string)
mmopen sthefile
end property
读取属性wait的值
msgbox multimedia.wait
property get wait() as boolean
wait = bwait
end property
设置等待属性
用法:multimedia.wait=true
property let wait(bwaitvalue as boolean)
bwait = bwaitvalue
end property
获得长度值
property get length() as single
dim nreturn as long, nlength as integer
dim slength as string * 255
if salias = “” then
length = 0
exit property
end if
nreturn = mcisendstring(“status ” & salias & ” length”, slength, 255, 0)
nlength = instr(slength, chr$(0))
length = val(left$(slength, nlength – 1))
end property
property let position(byval nposition as single)
mmseek nposition
end property
获取当前位置
property get position() as single
dim nreturn as integer, nlength as integer
dim sposition as string * 255
if salias = “” then exit property
nreturn = mcisendstring(“status ” & salias & ” position”, sposition, 255, 0)
nlength = instr(sposition, chr$(0))
position = val(left$(sposition, nlength – 1))
end property
当前打开文件的状态
有以下几种:playing paused stopped
property get status() as string
dim nreturn as integer, nlength as integer
dim sstatus as string * 255
if salias = “” then exit property
nreturn = mcisendstring(“status ” & salias & ” mode”, sstatus, 255, 0)
nlength = instr(sstatus, chr$(0))
status = left$(sstatus, nlength – 1)
end property
从头开始播放
public sub mmrestart()
dim nreturn as long
if salias = “” then exit sub
mmseek 0
mmplay
end sub
类的初始化
private sub class_initialize()
salias = “” 别名初值为空
end sub
关闭打开的多媒体设备
当该类的对象所在的窗体(或模块)卸载时,自动调用该过程
private sub class_terminate()
mmclose
end sub
—————————————————-
[用法]
1、
比如要在窗体上播放一个动画,只需3个语句就搞定。
dim mmavi as new mmedia
mmavi.mmopen “g:\resource\avi\test.avi”, me.hwnd
mmavi.mmplay
2、循环播放
private sub timer1_timer()
dim s as string
s = “当前文件:” & mmavi.filename & vbcrlf & “当前位置:” & mmavi.position _
& “总长度:” & mmavi.length & “当前状态:” & mmavi.status
label1.caption = s
if mmavi.status = “stopped” then mmavi.mmrestart
end sub
3、同时播放几个文件(类型可以相同、可以不同)
在form1中加入private mmwave(1) as new mmedia
在需要播放的地方加上:
mmwave(0).mmopen “g:\resource\wave\m16.wav”
mmwave(1).mmopen “g:\resource\wave\welcom98.wav”
mmwave(0).mmplay
mmwave(1).mmplay
4、将动画放入一个圆形区域播放
dim hr as long
dim usew&, useh&
dim mmavi as new mmedia
usew& = frame1.width / screen.twipsperpixelx
useh& = frame1.height / screen.twipsperpixely
usew = useh
hr& = createellipticrgn(0, 0, usew, useh)
call setwindowrgn(frame1.hwnd, hr, true)
mmavi.mmopen “g:\resource\avi\start.avi”, frame1.hwnd
mmavi.mmplay
………………………………