Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Sound volume control in VB.NET

Hi there

I am trying to program an application in VB.NET, where I can play wav files by adjusting a trackbar. I would like to make a simple scrollbar or trackbar, which controls the sound volume of my pc speaker...

Any ideas? :)

Best regards
Wael


Here is some of the code which I have written:

' *******************************************
Public Class SoundClass
        Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer

        Public Sub PlaySoundFile(ByVal filename As String)
            PlaySound(filename, Nothing, &H20000 Or &H1)
        End Sub

End Class

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

          Dim SoundInst As New SoundClass
          SoundInst.PlaySoundFile("C:\1.wav")

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

Wael Al-Assadi
Tuesday, November 25, 2003

Check out the Windows Multimedia SDK docs:
http://msdn.microsoft.com/library/en-us/multimed/htm/_win32_windows_multimedia_start_page.asp

The waveOutSetVolume() function looks promising for what you want to do.

http://www.partware.com/ebooks/api/ref/funcc.html#audio has the VB Declares...

Duncan Smart
Tuesday, November 25, 2003

Thanks alot Duncan Smart

An other question:

I have now declared "waveOutSetVolume". "waveOutGetVolume" and "WaveOutGetNumDevs" in a calls which I called "SoundClass"

Then I added a trackbar from the toolbox and wrote the following code to test if the wav volume changes, when I change the position of the trackbar arrow:

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

        Dim SoundInst As New SoundClass
        Dim retval As Long

        TrackBar1.Maximum = 65
        TrackBar1.TickFrequency = 1
        TrackBar1.LargeChange = 5
        TrackBar1.SmallChange = 1

        TextBox1.Text = TrackBar1.Value

        SoundInst.PlaySoundFile("C:\1.wav")

        retval = SoundClass.waveOutGetNumDevs()

        If TrackBar1.Value > 5 Then
            If retval >= 1 Then
                retval = SoundClass.waveOutSetVolume(&H7FFF, 0)
                SoundInst.PlaySoundFile("C:\1.wav")
            End If
        End If


    End Sub

' *********************************************

This code works so far. Now I would like to adjust the sound of my file "1.wav" when I move the arrow of the trackbar, where 65 should give the max volume and 0 the lowest volume.

I have tried to make some if and if else coding, where I ask if the trackbar value is equal to some value, then it should play different volume levels by changing the numbers inside the waveOutSetVolume function. But it's not really working. 

Any ideas?

Best regards Wael

Wael Al-Assadi
Wednesday, November 26, 2003

Looks like you just need to use some very basic maths to have your scrollbar's range equate to the range expected by the waveOutSetVolume(). I'm sure if you think about it some more, you can work it out.

Duncan Smart
Thursday, November 27, 2003

Hi again.

Thanks for the reply. I have done it and it's working perfectly :)

Have a nice time everyone :)

Best regards
Wael

Wael Al-Assadi
Thursday, November 27, 2003

*  Recent Topics

*  Fog Creek Home