locked
VB.NET: Control the System Volume, mute, and output the current level to the user RRS feed

  • Question

  • Hello

    I have searched the web for code relating to changing the system volume, muting the system volume, and outputing the current level of the volume as an integer, using VB.NET.

    All the code that I have found either doesn't work, or is very long.

    Does anyone have relatively small code to achieve this.

    Thank you
    Aaron

    Thursday, April 12, 2012 4:11 PM

Answers

  • Hi AaronMcH,

    Welcome to the MSDN forum.

    I’d like to share the smaller code. But this code just up, down and mutes the volume. It can’t show the integer currently.  You can create a new application, add three buttons and the below code and have a try.

    Imports System.Runtime.InteropServices
    Public Class Form1
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function
        Const WM_APPCOMMAND As UInteger = &H319
        Const APPCOMMAND_VOLUME_UP As UInteger = &HA
        Const APPCOMMAND_VOLUME_DOWN As UInteger = &H9
        Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_UP * &H10000)
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_DOWN * &H10000)
        End Sub
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H200EB0, APPCOMMAND_VOLUME_MUTE * &H10000)
        End Sub
    End Class

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Mark Liu-lxf Friday, April 20, 2012 7:07 AM
    Monday, April 16, 2012 5:45 AM
  • Have you tried using the below Class? Unfortunately the .NET code requires use of API function calls or external libraries to perform these operations.

    http://blogs.vbcity.com/drydo/archive/2005/06/07/2038.aspx


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Mark Liu-lxf Friday, April 20, 2012 7:07 AM
    Thursday, April 12, 2012 4:23 PM

All replies

  • Have you tried using the below Class? Unfortunately the .NET code requires use of API function calls or external libraries to perform these operations.

    http://blogs.vbcity.com/drydo/archive/2005/06/07/2038.aspx


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Mark Liu-lxf Friday, April 20, 2012 7:07 AM
    Thursday, April 12, 2012 4:23 PM
  • Hi Paul

    I had seen that, I was hoping there would be something a little bit smaller.

    Thanks
    Aaron

    Thursday, April 12, 2012 5:52 PM
  • Hi AaronMcH,

    Welcome to the MSDN forum.

    I’d like to share the smaller code. But this code just up, down and mutes the volume. It can’t show the integer currently.  You can create a new application, add three buttons and the below code and have a try.

    Imports System.Runtime.InteropServices
    Public Class Form1
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function
        Const WM_APPCOMMAND As UInteger = &H319
        Const APPCOMMAND_VOLUME_UP As UInteger = &HA
        Const APPCOMMAND_VOLUME_DOWN As UInteger = &H9
        Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_UP * &H10000)
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_DOWN * &H10000)
        End Sub
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H200EB0, APPCOMMAND_VOLUME_MUTE * &H10000)
        End Sub
    End Class

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Mark Liu-lxf Friday, April 20, 2012 7:07 AM
    Monday, April 16, 2012 5:45 AM
  • Hi AaronMcH,

    We haven’t heard from you for several days. I’d like to mark the helpful replies as answer firstly. If you have any additional questions, you also can unmark the replay and post your question here. 

    Sorry for any inconvenience and have a nice day.

     


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 20, 2012 7:08 AM
  • hi this code works very very well and it works in windows 7 Cool thank 
    Thursday, May 3, 2012 10:51 AM
  • Hello Mark,

    i just register me for this forum to say Thank You !!! to you...

    I searched the web for couples of hours, lookin for some tiny piece of code, like yours...

    Bravo... ;)

    For security reasons, i dont like to download certain tools, extensions, or selfmade dll...

    Anyway, you did it... ;)

    Greetz from .de

    Monday, January 7, 2013 3:13 AM
  • Thanks a lot, Mark!!!!

    Wednesday, March 6, 2013 4:56 AM
  • This code uses a single scrollbar to increase and decrease volume.

    Option Strict On
    
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
        Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
        Private Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
        Private Const WM_APPCOMMAND As Integer = &H319
    
        <DllImport("user32.dll")> Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function
    
        Dim HScroll3 As Integer = 0
        Dim Count As Integer = 0
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
        End Sub
    
    
        Private Sub HScrollBar3_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar3.Scroll
            If Count = 1 Then
                If HScrollBar3.Value > HScroll3 Then
                    SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
                End If
                If HScrollBar3.Value < HScroll3 Then
                    SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_DOWN))
                End If
            End If
            HScroll3 = HScrollBar3.Value
            Count = 1
        End Sub
    End Class
    


    You've taught me everything I know but not everything you know.

    Wednesday, March 6, 2013 7:20 AM
  • How can I get current volume control? and play an audio file in loop at 100% volume and when user click a button set back the volume control to initial value.

    Saturday, March 28, 2015 2:47 PM