走近VB.Net(十) 播放聲音文件
2024-07-10 13:01:28
供稿:網(wǎng)友
因為考慮到有部分初學(xué)者vb6沒有用過,所以我在源碼中對每一句代碼都進行了注釋。
首先如下添加控件:
添加三個button控件,寫代碼如下:
imports system.componentmodel '引用 族名 system.componentmodel
imports system.drawing '引用族名 system.drawing
imports system.winforms '引用族名 system.winforms
'引用族名的目的是在調(diào)用他的子類時不需要寫出族名,如system.drawing.color寫成color
public class form1
inherits system.winforms.form ' inherits是從已有的一個類繼承,得到這個類已定義的方法
public sub new()
mybase.new() 'mybase是指代父類 mybase.new是調(diào)用父類的new過程,new是新建一個對象,對象是存在內(nèi)存的,類是并不存在內(nèi)存的
form1 = me '這樣,下面的me關(guān)鍵字 就被認定為form1
initializecomponent() '這下面的代碼是在新建一個form1的實例時執(zhí)行的初始化動作
call me_load() '這是自定義的一個子過程
end sub
'form overrides dispose to clean up the component list.
overrides public sub dispose()
axmmcontrol1.command = "close" '關(guān)閉聲音資源,這個代碼一定要放在dispose過程的前面
mybase.dispose() 'dispose是清除對象
components.dispose() '清空組件
end sub
'下面的代碼是對屬性的描述,是系統(tǒng)自已生成的,不要隨意修改
#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
'上面的代碼是對屬性的描述,是系統(tǒng)自已生成的,不要隨意修改
protected sub button1_click(byval sender as object, byval e as system.eventargs)
axmmcontrol1.from = 8815 '從3000毫秒開始
axmmcontrol1.to =15624 '從6000毫秒結(jié)束
axmmcontrol1.command = "play" '播放
end sub
protected sub button3_click(byval sender as object, byval e as system.eventargs)
me.dispose() '調(diào)用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毫秒結(jié)束
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為當(dāng)前目錄的意思
axmmcontrol1.command = "open" '打開載入的文件
axmmcontrol1.visible = false '使控件不可見,在后臺工作
end sub
end class
至于從幾毫秒開始到幾毫秒結(jié)束,是把不同的聲音文件使用如acoustica.exe這樣的聲音編輯程序整理成一個文件,記住先使用windows的錄音機錄下來,這樣生成的文件較小,把一些雜音,空白都剪切掉,這樣就是很小了。
本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。