Results 1 to 2 of 2

Thread: VB.NET Simple Read and Set Master Volume Control

Threaded View

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    VB.NET Simple Read and Set Master Volume Control

    Whilst searching for help when looking for a way to read and adjust the Volume Control which can be accessed via the 'Speaker' Icon (usually) bottom right of the screen I came across many posts looking for the same information and asking similar questions, the answers are usually long (very long) and tedious and for a beginner such as myself pretty baffling. I've always thought there must be a fairly straightforward answer to the question 'How can I control the Master Volume' ?
    This short code is a reasonably simple answer, on the Form1 of a new project place three Buttons, a TextBox and a Label.
    Arrange the buttons in a column on the left of the Form1, place the TextBox in-line with Button1 and Label1 in-line with Button2.
    Open the Form1 code page and delete everything there... Start with a totally empty page, then copy and paste this code onto that page: -

    Code:
    '	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
    You will need to add a reference to CoreAudio.dll before this code will work, a search of your HDD may not find this file in which case you'll need to download it from the web, it can be done for free, but dodging all the stuff web sites also want you to take on board may be a bit tricky... Be warned !
    Once you have the file, navigate to 'Solution Explorer' and Right Click your project's name, click 'Add Reference'.
    Then click the 'Browse' tab and navigate to the folder containing 'CoreAudio.Dll'.
    Select 'CoreAudio.dll' and click 'OK', this should install the file in your project.

    Clicking button 2 will display the current setting of the Master Volume. You should be able to check this by passing your cursor over the 'Speaker' icon.
    Enter a figure into the TextBox and click button 1 the volume level will be adjusted (and echoed in the textbox) There is no need to add '%' when you enter the figure but it'll do no harm if you do. You can check with button 2 and the icon.

    I think you will find the coding easy enough to follow to modify for your own project.

    Poppa.
    Last edited by Poppa Mintin; Aug 15th, 2012 at 10:36 AM.
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width