checking and changing system volume vb.net

Dim vol As New Sound(path_to_nircmd)
vol.setVol(50)

4.63
8
Phoenix Logan 186120 points

                                    '	NOTES.
'1 On Form1 this project requires: 3 Buttons, 1 TextBox and 1 Label.
'2 Navigate to Solution Explorer and Right Click the project name. 
'3 Click 'Add Reference'.
'4 Click the 'Browse' tab.
'5 Navigate to the folder containing 'CoreAudio.Dll' 
'6 Select 'CoreAudio.dll' and click 'OK'.

Imports CoreAudio

Public Class Form1

    Dim Svol As Integer = 0   'Select Volume.  Global variable.

    Private Sub Form1_Load() Handles MyBase.Load

        Button1.Text = "SetVol"
        Button2.Text = "READ"
        Button3.Text = "EXIT"
        Label1.Text = ""
    End Sub

    Private Sub Button1_Click() Handles Button1.Click
     ' Click this button to set a new volume level. First type the
     ' required level into TextBox1, as a percentage 0 to 100 (%)

        Svol = Val(TextBox1.Text)             ' Get the required value
        If Svol < 0 Then Svol = 0              ' Ensure it's within limits
        If Svol > 100 Then Svol = 100
        TextBox1.Text = Svol.ToString       ' Echo the level back into TextBox1
        SetVol()                                       ' Call the subroutine.

    End Sub

    Private Sub Button2_Click() Handles Button2.Click
     ' Display the current volume as collected by GetVol Function

        Label1.Text = "Current Volume Level = " & GetVol().ToString & "%"
    End Sub

    Private Sub Button3_Click() Handles Button3.Click

        Me.Close()                 ' Close and exit the program.
    End Sub

    Private Function GetVol() As Integer        'Function to read current volume setting
        Dim MasterMinimum As Integer = 0
        Dim DevEnum As New MMDeviceEnumerator()
        Dim device As MMDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
        Dim Vol As Integer = 0

        With device.AudioEndpointVolume
            Vol = CInt(.MasterVolumeLevelScalar * 100)
            If Vol < MasterMinimum Then
                Vol = MasterMinimum / 100.0F
            End If
        End With
        Return Vol
    End Function

    Private Sub SetVol()

        Dim DevEnum As New MMDeviceEnumerator()
        Dim device As MMDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)

        device.AudioEndpointVolume.MasterVolumeLevelScalar = Svol / 100.0F

    End Sub

End Class

4.63 (8 Votes)
0
4
1
Awgiedawgie 440215 points

                                    Public Class Sound
    Dim nircmd As String
    Const MAXVOL As Integer = 65535

    Public Sub New(ByVal nircmd_location As String)
        nircmd = nircmd_location
    End Sub

    Public Sub setVol(ByVal level As Integer)

        Dim p As New ProcessStartInfo
        p.FileName = nircmd
        p.Arguments = "setsysvolume " & (MAXVOL * (level / 100)).ToString
        Process.Start(p)

    End Sub
End Class

4 (1 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source