checking and changing system volume vb.net code example
Example 1: checking and changing system volume vb.net
' 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
Example 2: checking and changing system volume vb.net
Dim vol As New Sound(path_to_nircmd)
vol.setVol(50)
Example 3: checking and changing system volume vb.net
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