当前位置:编程学习 > C#/ASP.NET >>

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

答案:因为考虑到有部分初学者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的录音机录下来,这样生成的文件较小,把一些杂音,空白都剪切掉,这样就是很小了。

上一个:走近VB.Net(十一) 随心所欲窗体外观(skin精彩源码)
下一个:走近VB.Net(九) AxAgent动画窗体的实现

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,