日期:2012-12-12  浏览次数:20416 次

第二步分,如何控制音量的大小

如果想控制音量大小跟左右声道或者播放速度,那么创建buf之前还需要做一些工作

创建一个bufferdescription对象
Dim desc As New BufferDescription

设置desc的属性

desc.ControlPan = True
desc.ControlVolume = True
desc.ControlFrequency = True
(当然这里还有其他功能,以后再说,3D那里我还没有研究,一步一步的来)

完成之后,可以创建了,跟一开始的不一样了,多了一个desc
Buf = New SecondaryBuffer("G:\MEDIA\WAV\rod2.wav", desc, ad)

(微软:你要控制音量大小可以说嘛,虽然你很深情的望着我,但是你不说我会怎么知道呢.....)

记住,上面仅仅是说明你可以改面音量大小了,但是还没有提到怎么改变.

Buf.Volume=0 '这个声音就是最大了
buf.volume=-5000 '微软给的例子是-5000 实际上 -50的时候就听不见什么动静了..

buf.pan=0 '两边声道均衡 -10000 跟 10000 是左右声道

Buf.Frequency= desc.Format.SamplesPerSecond '默认的声音 44khz 就是 44100 这个是速度

OK,速度一般不需要多大的变化.偶尔玩玩是可以,实际意义就不大了.

改动之后的代码

======================================
Dim ad As Device
Dim Buf As SecondaryBuffer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ad = New Device
Dim desc As New BufferDescription
desc.ControlPan = True
desc.ControlVolume = True
desc.ControlFrequency = True
desc.Flags = BufferDescriptionFlags.GlobalFocus '全局的,上一篇有介绍


ad.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)
Buf = New SecondaryBuffer("G:\MEDIA\WAV\rod2.wav", desc, ad)
Buf.Play(0, BufferPlayFlags.Looping)

Label5.Text = Buf.Frequency.ToString
End Sub

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Buf.Volume = 0 '最大就是0
Label2.Text = Buf.Volume.ToString
End Sub

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
' Buf.Volume = -50 * 100
Buf.Volume = -5 * 100
Label2.Text = Buf.Volume.ToString

End Sub

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If e.Button = MouseButtons.Left Then
Buf.Pan = -20 * 500
Else
Buf.Pan = 20 * 500
End If
Label4.Text = Buf.Pan.ToString
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Buf.Pan = 0
Label4.Text = Buf.Pan.ToString

End Sub
======================================