电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
用VB播放Avi、Wave、midi文件-.NET教程,VB.Net语言
作者:网友供稿 点击:214
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 

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
………………………………


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·经典收藏之 - C++内存管理详解-.NET教程,C#语言
·Master Page 初探-.NET教程,评论及其它
·GDI+编程10个基本技巧-.NET教程,评论及其它
·VB.NET中让Textbox只能输入数字(二)-.NET教程,VB.Net语言
·stl应用小问题-.NET教程,评论及其它
·WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换-ASP教程,系统相关
·打造自己的专业图像工具-Visual C++ 2005图像编程系列【三】-.NET教程,C#语言
·.Net中常见问题及解决方法归类-.NET教程,.NET Framework
·Lex和Yacc从入门到精通(3)--一个极其简单的lex和yacc程序-.NET教程,评论及其它
·VB下几个非常有用的函数-.NET教程,VB.Net语言

最新文章
·VC#初学入门:第一个Windows程序
·ASP.NET 2.0-选用DataSet或DataReader
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·asp.net 2.0 中加密web.config 文件中的配置节
·关于ASP.NET调用JavaScript的实现
·如何实现ASP.NET网站个性化
·Acegi安全系统的配置-.NET教程,评论及其它
·Spring安全系统:Acegi Security Acegi简介-.NET教程,评论及其它
·Biztalk 开发之 架构和实例的验证-.NET教程,评论及其它


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号