vb.net2008 Direct编程(三)音乐播放器——第二部分

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Enb_DS As New BufferDescription

        Enb_DS.ControlVolume = True

        Enb_DS.ControlPan = True

        dev.SetCooperativeLevel(Me, CooperativeLevel.Normal)

End Sub

接着我们写播放事件,我也不多说废话了,不会的请看上一节。但在此一定要注意一个地方,就是在播放前先要吧sb实例化,而且播放时要把“播放”按钮的enabled改为false,预防重复,“停止”按钮enabled改为true,否则就停止不了啦,具体代码看这里:

        sb = New SecondaryBuffer(TextBox1.Text, dev)

        sb.Play(0, BufferPlayFlags.Looping)

        Button1.Enabled = False

        Button2.Enabled = True

写完了播放代码,接下来就是停止了,(问:“停止”代码怎么写?答:“超级简单!”),不是我忽悠你,如果过你“停止”学不会的话,你的“师傅”我立马跳楼……废话不多说,就一句代码:

        sb.Stop()

简单吧,千万别忘了改变“播放”,“停止”按钮的enable属性哦~。具体代码请看这里:

        sb.Stop()

        Button1.Enabled = True

        Button2.Enabled = False

写完了停止了,接下来要写“高难度”代码了(哭:“我好怕哦!”师傅:从东方明珠上掉下来ing)就两句代码,信不信由你喽~先看调节声音的代码,在direct的sound中有一个方法“Volume”通过它可以直接调整音量大小,不过它的值比较怪——最小声音是-5000,最大声音是0,所以我一开始要求你把trackbar的minimum设为-5000;maximum的值设为0,具体本功能的实现代码look下面:

        sb.Volume = TrackBar1.Value

再来看声道,也是一句代码,只需要改变sb的pan这个值就可以了,这个值的精确度可是非常非常非常高的,全左声道是-10000,全右声道是10000,相差两万,十分精确,设好了trackbar的值,我们开始写代码喽~具体代码下面见:

        sb.Pan = TrackBar2.Value

好了,到了发全代码的时间了,你在这一步之前成功了么?

Imports Microsoft.DirectX.DirectSound

Public Class Form1

    Dim dev As New Device

Dim sb As SecondaryBuffer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Enb_DS As New BufferDescription

        Enb_DS.ControlVolume = True

        Enb_DS.ControlPan = True

        dev.SetCooperativeLevel(Me, CooperativeLevel.Normal)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        sb = New SecondaryBuffer(TextBox1.Text, dev)

        sb.Play(0, BufferPlayFlags.Looping)

        Button1.Enabled = False

        Button2.Enabled = True

    End Sub

    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll

        sb.Volume = TrackBar1.Value

    End Sub

    Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll

        sb.Pan = TrackBar2.Value

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        sb.Stop()

        Button1.Enabled = True

        Button2.Enabled = False

    End Sub

End Class

尽情发挥你的想象力,让这个程序更加完善吧,本节的错误和疑问下边的回复中见~886

转自《 http://hi.baidu.com/752664788/blog/item/2051393587a1a5325bb5f5a5.html

你可能感兴趣的:(vb.net2008 Direct编程(三)音乐播放器——第二部分)