none
Visual Basic - How to get volume level in Windows Vista/7

    Question

  • How can I get the master volume level in Visual Basic? Must work on Vista and 7.


    Thanks!
    Grant

    Sunday, April 18, 2010 3:27 PM

Answers

All replies

  • Try this code:

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Text
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices

    Public Class Form1

        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container()
            Me.TrackBar1 = New System.Windows.Forms.TrackBar()
            Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
            CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'TrackBar1
            '
            Me.TrackBar1.Location = New System.Drawing.Point(12, 12)
            Me.TrackBar1.Name = "TrackBar1"
            Me.TrackBar1.Orientation = System.Windows.Forms.Orientation.Vertical
            Me.TrackBar1.Size = New System.Drawing.Size(45, 240)
            Me.TrackBar1.TabIndex = 0
            '
            'Timer1
            '
            '
            'Form1
            '
            Me.ClientSize = New System.Drawing.Size(108, 264)
            Me.Controls.Add(Me.TrackBar1)
            Me.Name = "Form1"
            CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)
            Me.PerformLayout()

        End Sub

        <DllImport("winmm.dll")> _
        Public Shared Function waveOutGetVolume(ByVal hwo As IntPtr, ByRef dwVolume As UInteger) As Integer
        End Function

        <DllImport("winmm.dll")> _
        Public Shared Function waveOutSetVolume(ByVal hwo As IntPtr, ByVal dwVolume As UInteger) As Integer
        End Function

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            On Error Resume Next
            Timer1.Enabled = True
        End Sub

        Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            On Error Resume Next
            Dim NewVolume As Integer = ((UShort.MaxValue / 10) * TrackBar1.Value)
             Dim NewVolumeAllChannels As UInteger = ((CUInt(NewVolume) And &HFFFF) Or (CUInt(NewVolume) << 16))
             waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels)
        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            On Error Resume Next
            Dim CurrVol As UInteger = 0
            waveOutGetVolume(IntPtr.Zero, CurrVol)
            Dim CalcVol As UShort = CUShort((CurrVol And &HFFFF))
            TrackBar1.Value = CalcVol / (UShort.MaxValue / 10)
        End Sub
    End Class
      
    by naldo

    admsystems@live.com

    Tuesday, April 20, 2010 7:10 AM
  • Hello grantman16,
    Thanks for your post.
    (1) If you would like to get master volume, I suggest you to check out the following threads, I hope they are helpful.
    http://blogs.vbcity.com/drydo/archive/2005/06/07/2038.aspx
    http://www.devx.com/vb2themax/Tip/19040
    (2) Here is also a thread where the author asks the similar question, but it is about C#. If you understand C#, it could be very useful. Or you may try to make a converation.
    http://social.msdn.microsoft.com/forums/en-US/windowspro-audiodevelopment/thread/cb3de43e-3abb-4428-b58a-dc3e5f29db30/
    Convert C# to VB.NET: http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

    For more information, you can see:
    (1)GetMasterVolume method retrieves the client volume level for the audio session.
    http://msdn.microsoft.com/en-us/library/dd316533(VS.85).aspx
    (2)SetMasterVolume: The SetMasterVolume method sets the master volume level for the audio session
    http://msdn.microsoft.com/en-us/library/ms679141(VS.85).aspx
    http://msdn.microsoft.com/en-us/library/ms678759.aspx
    I hope they are helpful.
    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Friday, April 23, 2010 2:48 AM
  • Thanks for your posts, but unfortunately, I can't get anything to work.

    Naldopr, I've tried using "waveOutGetVolume", but it always returns the same value no matter what I set my volume to.

    Liliane, your first two links are for setting the master volume, not getting it. The other links are for C++/C#, and I haven't been able to get them to work in VB.

    I'm thinking the "GetMasterVolume" function (http://msdn.microsoft.com/en-us/library/dd316533(VS.85).aspx ) would work, but I don't know how to access "ISimpleAudioVolume" from VB. Does anyone know how?


    Thanks!
    Grant

    Friday, April 23, 2010 10:20 PM
  • @grantman16 - Here is a website that will convert C# code to VB.NET code.  There are other sites out there that will do the same thing.  I've never had a problem with this one.

         http://www.developerfusion.com/tools/convert/csharp-to-vb

    :)


    Doug

    SEARCH ... then ask
    Sunday, April 25, 2010 10:52 PM