这次的例子是一个简易的播放器,该播放器能够实现播放的声音文件,并可以停止、调节音量和声道。
===============华丽的分割线=============== DirectX与VB.NET编程(三)简单的播放器
学习要点:
·停止声音的播放;
·调节声音的音量和声道并掌握音量和声道的取值范围。
===============华丽的分割线===============
首先在窗体上摆放如下控件:一个文本框(TextBox)两个按钮(Button)和两个滑槽(TrackBar)。窗口设计请参考图1或者最后的代码。
===============华丽的分割线===============
首先,在程序开始时初始化设备并将按钮归位,在Form_Load函数中加入如下代码,让Button1(开始)按钮保持可用,而Button2(停止)按钮不可用。
Dev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)
Button1.Enabled = True
Button2.Enabled = False
在Button1(播放)按钮的Button1_Click方法中加入代码。
下面开始初始化缓冲区,但是需要注意的是缓冲区拥有两个属性,ControlVolume与ControlPan两个属性,这两个属性控制着缓冲区的声音能否被调整音量或声道,如果该值为FALSE的话,调整音量或声道则会报错,因此,需要在初始化缓冲区前声明一个缓冲区描述,然后通过该描述构造出来的缓冲区将具有描述中的属性。声明描述并设置它的语句如下:
Des.ControlVolume = True
Des.ControlPan = True
然后使用该描述初始化缓冲区并播放声音:
SB.Play(0, BufferPlayFlags.Looping)
设置按钮可用度并调用滑槽的滚动事件,让滑槽的初值对声音有效:
Button2.Enabled = True
TrackBar1_Scroll(0, Nothing)
TrackBar2_Scroll(0, Nothing)
===============华丽的分割线===============
缓冲区的Stop()方法可以停止播放声音,因此在Button2(停止)按钮的Button2_Click方法中加入如下代码即可:
SB.Dispose()
Button1.Enabled = True
Button2.Enabled = False
===============华丽的分割线===============
下面是调整音量和声道,调整音量使用缓冲区的Volume属性,声道使用Pan属性,通过滑槽来调整吧!
需要注意的是音量的最小值是-5000,最大值是0;声道的完全左声道为-10000,中间为0,完全右声道为10000。
因此下面是两个滑槽的代码:
SB.Volume = -5000 + TrackBar1.Value * 50
End Sub
SB.Pan = -10000 + TrackBar2.Value * 200
End Sub
===============华丽的分割线===============
程序的所有代码如下:
Imports Microsoft.DirectX.DirectSound
Dim SB As SecondaryBuffer
MyBase.New()
InitializeComponent()
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TrackBar2 As System.Windows.Forms.TrackBar
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.TrackBar1 = New System.Windows.Forms.TrackBar
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.TrackBar2 = New System.Windows.Forms.TrackBar
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 8)
Me.TextBox1.Name = "TextBox1" DirectX与VB.NET编程(三)简单的播放器
Me.TextBox1.Size = New System.Drawing.Size(272, 21)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "D:\魔兽争霸2\DRIVERS\DIGTEST.WAV"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 40)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "播放"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(96, 40)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(80, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "停止"
'
'TrackBar1
'
Me.TrackBar1.Location = New System.Drawing.Point(32, 64)
Me.TrackBar1.Maximum = 100
Me.TrackBar1.Value = 100
Me.TrackBar1.Name = "TrackBar1"
Me.TrackBar1.Size = New System.Drawing.Size(104, 45)
Me.TrackBar1.TabIndex = 3
Me.TrackBar1.TickStyle = System.Windows.Forms.TickStyle.None
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 72)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(32, 16)
Me.Label1.TabIndex = 4
Me.Label1.Text = "音量"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(144, 72)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(32, 16)
Me.Label2.TabIndex = 5
Me.Label2.Text = "声道"
'
'TrackBar2
'
Me.TrackBar2.Location = New System.Drawing.Point(168, 64)
Me.TrackBar2.Maximum = 100
Me.TrackBar2.Value = 50
Me.TrackBar2.Name = "TrackBar2"
Me.TrackBar2.Size = New System.Drawing.Size(104, 45)
Me.TrackBar2.TabIndex = 6
Me.TrackBar2.TickStyle = System.Windows.Forms.TickStyle.None
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(288, 110)
Me.Controls.Add(Me.TrackBar2)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TrackBar1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "播放器"
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Dev = New Device
Dev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)
Button1.Enabled = True
Button2.Enabled = False
End Sub
Dim Des As New BufferDescription '初始化描述
Des.ControlVolume = True '允许修改音量
Des.ControlPan =True '允许修改声道
SB = New SecondaryBuffer(TextBox1.Text, Des, Dev) '通过描述初始化缓冲区
SB.Play(0, BufferPlayFlags.Looping) '播放声音
Button1.Enabled = False
Button2.Enabled = True
TrackBar1_Scroll(0, Nothing)
TrackBar2_Scroll(0, Nothing)
End Sub
SB.Stop() '停止播放声音
SB.Dispose() '回收缓冲区
Button1.Enabled = True
Button2.Enabled = False
End Sub
SB.Volume = -5000 + TrackBar1.Value * 50 '设置音量
End Sub
SB.Pan = -10000 + TrackBar2.Value * 200 '设置声道
End Sub
下面尝试下这个播放器吧~~在文本框里输入路径,点击播放就可以开始了!!使用耳机测试声道效果要明显些。感觉还爽么~~ DirectX与VB.NET编程(三)简单的播放器
下次讲3D音效的理论篇。