control of master volume ?

CdRsKuLL

Active member
Joined
Sep 26, 2006
Messages
40
Programming Experience
Beginner
Well.. I've looked and looked and can't seem to find a example of how to control windows 7 master volume. I would like to get the current volume / set the volume and also mute / unmute it.

I've found lots of examples of pre vista volume control but only a single c# which is no good to me beings a major newbie with vb.net (I've play with vb6 before now)

As far as I can see I need to do something with DefaultAudioEndpoint.. but what I'm not sure. I'm building a media player which is why I'm after controlling the master as well as my application.

Any help at all would be greatly appreciated.

thanks,

Steve
 
I found this Vista Core Audio API Master Volume Control - CodeProject, the sample download contains a library that you can reference and use in VB.
Below is a VB translation of the sample using the master volume control,
prerequisites:
  • add reference to the CoreAudioApi.dll
  • add a Trackbar control to form and name it tbMaster, set Maximum property to 100
Imports CoreAudioApi

Public Class FormMasterVolume

    Private device As MMDevice

    Private Sub FormMasterVolume_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load    
        Dim DevEnum As New MMDeviceEnumerator()
        device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
        AddHandler device.AudioEndpointVolume.OnVolumeNotification, AddressOf OnVolumeNotification
        tbMaster.Value = CInt(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100)
    End Sub

    Private Sub OnVolumeNotification(data As CoreAudioApi.AudioVolumeNotificationData)
        If Me.InvokeRequired Then
            Me.Invoke(New AudioEndpointVolumeNotificationDelegate(AddressOf OnVolumeNotification), data)
        Else
            tbMaster.Value = CInt(data.MasterVolume * 100)
        End If
    End Sub

    Private Sub tbMaster_Scroll(sender As System.Object, e As System.EventArgs) Handles tbMaster.Scroll
        device.AudioEndpointVolume.MasterVolumeLevelScalar = tbMaster.Value / 100.0F
    End Sub

End Class
 
Thanks for the reply. I've downloaded the sample and added the dll to my folder / referenced it and also copied the code into a class named FormMasterVolume. However I'm getting afew errors. I'm using VB.net 2010 Express (just learning slowly)

The errors are,
Private Sub FormMasterVolume_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Event 'Load' cannot be found

If Me.InvokeRequired Then
Me.Invoke(New AudioEndpointVolumeNotificationDelegate(AddressOf OnVolumeNotification), data)

I've not used the Invoke this before.. I assume it's similar to a hook in VB.

Many thanks for your help, I did find this example but with it being in c# I didnt look any closer.

Steve
 
ok, did a bit more playing and moved the code into the main form. It now seems to work although I've changed a couple of things..

I added reference to the CoreAudioApi.dll (dropping the dll in my project folder)
I added the Imports CoreAudioApi to the top of my form
I added the Private device As MMDevice to the top of the form

Then I just inserted these three subs :) Works a treat and I can confirm is does monitor and change the variable when the volume is changed outside my app. Result :)

VB.NET:
    Private Sub OnVolumeNotification(ByVal data As CoreAudioApi.AudioVolumeNotificationData)
        If Me.InvokeRequired Then
            Me.Invoke(New AudioEndpointVolumeNotificationDelegate(AddressOf OnVolumeNotification), data)
        Else
            Currentvolume = CInt(data.MasterVolume * 100)
        End If
    End Sub

    Private Sub SetVolume()
        device.AudioEndpointVolume.MasterVolumeLevelScalar = Currentvolume / 100.0F
    End Sub
    Private Sub GetVolume()
        Dim DevEnum As New MMDeviceEnumerator()
        device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
        AddHandler device.AudioEndpointVolume.OnVolumeNotification, AddressOf OnVolumeNotification
        Currentvolume = CInt(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100)
    End Sub
 
ok, did a bit more playing and moved the code into the main form.
Yes, the FormMasterVolume class I posted was the code for the form class.
 
Back
Top