首页 > 编程 > .NET > 正文

走近VB.Net(十) 播放声音文件

2024-07-10 13:01:28
字体:
来源:转载
供稿:网友
因为考虑到有部分初学者vb6没有用过,所以我在源码中对每一句代码都进行了注释。
首先如下添加控件:


添加三个button控件,写代码如下:

imports system.componentmodel '引用 族名 system.componentmodel

imports system.drawing '引用族名 system.drawing

imports system.winforms '引用族名 system.winforms

'引用族名的目的是在调用他的子类时不需要写出族名,如system.drawing.color写成color

public class form1

inherits system.winforms.form ' inherits是从已有的一个类继承,得到这个类已定义的方法



public sub new()

mybase.new() 'mybase是指代父类 mybase.new是调用父类的new过程,new是新建一个对象,对象是存在内存的,类是并不存在内存的



form1 = me '这样,下面的me关键字 就被认定为form1





initializecomponent() '这下面的代码是在新建一个form1的实例时执行的初始化动作

call me_load() '这是自定义的一个子过程



end sub



'form overrides dispose to clean up the component list.

overrides public sub dispose()

axmmcontrol1.command = "close" '关闭声音资源,这个代码一定要放在dispose过程的前面

mybase.dispose() 'dispose是清除对象

components.dispose() '清空组件

end sub



'下面的代码是对属性的描述,是系统自已生成的,不要随意修改

#region " windows form designer generated code "



'required by the windows form designer

private components as system.componentmodel.container

private withevents button3 as system.winforms.button

private withevents button2 as system.winforms.button

private withevents button1 as system.winforms.button

private withevents axmmcontrol1 as axmci.axmmcontrol



dim withevents form1 as system.winforms.form



'note: the following procedure is required by the windows form designer

'it can be modified using the windows form designer.

'do not modify it using the code editor.

private sub initializecomponent()

dim resources as system.resources.resourcemanager = new system.resources.resourcemanager(gettype(form1))



me.components = new system.componentmodel.container()

me.button1 = new system.winforms.button()

me.axmmcontrol1 = new axmci.axmmcontrol()

me.button3 = new system.winforms.button()

me.button2 = new system.winforms.button()



axmmcontrol1.begininit()



'@design me.trayheight = 0

'@design me.traylargeicon = false

'@design me.trayautoarrange = true

button1.location = new system.drawing.point(8, 8)

button1.size = new system.drawing.size(80, 32)

button1.tabindex = 1

button1.text = "button1"



axmmcontrol1.ocxstate = ctype(resources.getobject("axmmcontrol1.ocxstate"), system.winforms.axhost.state)

axmmcontrol1.tabindex = 0

axmmcontrol1.size = new system.drawing.size(236, 33)

axmmcontrol1.location = new system.drawing.point(32, 56)



button3.location = new system.drawing.point(208, 8)

button3.size = new system.drawing.size(80, 32)

button3.tabindex = 3

button3.text = "button3"



button2.location = new system.drawing.point(104, 8)

button2.size = new system.drawing.size(88, 32)

button2.tabindex = 2

button2.text = "button2"

me.text = "form1"

me.autoscalebasesize = new system.drawing.size(6, 14)

me.clientsize = new system.drawing.size(296, 29)



me.controls.add(button3)

me.controls.add(button2)

me.controls.add(button1)

me.controls.add(axmmcontrol1)



axmmcontrol1.endinit()

end sub



#end region

'上面的代码是对属性的描述,是系统自已生成的,不要随意修改

protected sub button1_click(byval sender as object, byval e as system.eventargs)

axmmcontrol1.from = 8815 '从3000毫秒开始

axmmcontrol1.to =15624 '从6000毫秒结束

axmmcontrol1.command = "play" '播放

end sub



protected sub button3_click(byval sender as object, byval e as system.eventargs)

me.dispose() '调用dispose退出程序,释放占用的资源

end sub



protected sub form1_click(byval sender as object, byval e as system.eventargs)



end sub



protected sub button2_click(byval sender as object, byval e as system.eventargs)

axmmcontrol1.from = 0 '从 0毫秒开始

axmmcontrol1.to = 8815 '从3000毫秒结束

axmmcontrol1.command = "play" '播放

end sub



protected sub axmmcontrol1_oledragdrop(byval sender as object, byval e as axmci.dmcievents_oledragdropevent)



end sub



public sub me_load()

me.backcolor = color.blanchedalmond '改变背景色为blanchedalmond

me.borderstyle = formborderstyle.none '改变窗体边框为none,没有标题栏

button1.text = "播放上段" : button2.text = "播放下段" : button3.text = "退出程序"

axmmcontrol1.timeformat = 0

axmmcontrol1.devicetype = "waveaudio" '定义播放*.wav格式

axmmcontrol1.filename = system.winforms.application.startuppath & "/" & "muli.wav" '载入文件, system.winforms.application.startuppath为当前目录的意思

axmmcontrol1.command = "open" '打开载入的文件

axmmcontrol1.visible = false '使控件不可见,在后台工作

end sub

end class

至于从几毫秒开始到几毫秒结束,是把不同的声音文件使用如acoustica.exe这样的声音编辑程序整理成一个文件,记住先使用windows的录音机录下来,这样生成的文件较小,把一些杂音,空白都剪切掉,这样就是很小了。

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表