+ Reply to Thread
Results 1 to 5 of 5

Adjusting master volume

Hybrid View

  1. #1
    Registered User
    Join Date
    07-03-2007
    Location
    Canberra, Australia
    MS-Off Ver
    2010
    Posts
    60

    Adjusting master volume

    Hi,

    I have searched this and other forums and I am unable to find any code that works correctly, I am trying to detect if the users volume is set to mute and if it is I want it to turn the sound on. I also need to adjust the volume levels.

    I have found this code that toggles the mute on/off but I need to detect if it is already muted

    Option Explicit
    Const VK_VOLUME_MUTE = &HAD
    Private Declare Sub keybd_event Lib "user32" _
       (ByVal bVk As Byte, ByVal bScan As Byte, _
       ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    
    Sub Auto_Open()
    keybd_event VK_VOLUME_MUTE, 0, 1, 0
    End Sub
    I know it has something to do with API's but I am not sure how to access these

    Thanks for your help
    Kerno

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good morning Kerno

    There's a piece of code here from KeepITCool which seems to work like a treat. Copy it into an empty module, and use this formula to call it :
    =GetMasterMuteState()

    http://www.pcreview.co.uk/forums/thread-1008148.php

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    07-03-2007
    Location
    Canberra, Australia
    MS-Off Ver
    2010
    Posts
    60
    Hi dominicb,

    Thanks for your reply, the code to adjust the volume up or down works great but I cannot seem to get the code to check the mute state,
    I copied the function
    Option Explicit
    Function GetMasterMuteState() As Boolean
    ' This function reads the state of the masterMute control
    Dim hMixer As Long ' mixer handle
    
    Dim mxc As MIXERCONTROL
    Dim mxl As MIXERLINE
    Dim mxlc As MIXERLINECONTROLS
    Dim mxcd As MIXERCONTROLDETAILS
    Dim mxcdMute As MIXERCONTROLDETAILS_BOOLEAN
      
    Dim hMem As Long
    Dim rc As Long
    Dim iErr As Integer
    
    ' Open the mixer with deviceID 0.
    rc = mixerOpen(hMixer, 0, 0, 0, 0)
    If ((MMSYSERR_NOERROR <> rc)) Then iErr = 1: GoTo theExit
    
    mxl.cbStruct = Len(mxl)
    mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
    
    ' Obtain a line corresponding to the component type
    rc = mixerGetLineInfo(hMixer, mxl, _
    MIXER_GETLINEINFOF_COMPONENTTYPE)
    If (MMSYSERR_NOERROR <> rc) Then iErr = 2: GoTo theExit
    
    
    mxlc.cbStruct = Len(mxlc)
    mxlc.dwLineID = mxl.dwLineID
    mxlc.dwControl = MIXERCONTROL_CONTROLTYPE_MUTE
    mxlc.cControls = 1
    mxlc.cbmxctrl = Len(mxc)
    
    ' Allocate a buffer for the control
    hMem = GlobalAlloc(&H40, Len(mxc))
    mxlc.pamxctrl = GlobalLock(hMem)
    mxc.cbStruct = Len(mxc)
    
    ' Get the control
    rc = mixerGetLineControls(hMixer, mxlc, _
    MIXER_GETLINECONTROLSF_ONEBYTYPE)
    If (MMSYSERR_NOERROR <> rc) Then iErr = 3: GoTo theExit
    'Copy into mxc structure
    CopyStructFromPtr mxc, mxlc.pamxctrl, Len(mxc)
    GlobalFree (hMem)
    hMem = 0
    
    'Get the controldetails
    mxcd.cbStruct = Len(mxcd)
    mxcd.dwControlID = mxc.dwControlID
    mxcd.cChannels = 1
    mxcd.Item = 0
    mxcd.cbDetails = Len(mxcdMute)
    
    ' Allocate a buffer for the controldetails
    hMem = GlobalAlloc(&H40, Len(mxcdMute))
    mxcd.paDetails = GlobalLock(hMem)
    
    'Get the controldetailvalue
    rc = mixerGetControlDetails(hMixer, mxcd, _
    MIXER_OBJECTF_HMIXER Or MIXER_GETCONTROLDETAILSF_VALUE)
    If (MMSYSERR_NOERROR <> rc) Then iErr = 4: GoTo theExit
    ' Copy into mxcdMute structure
    CopyStructFromPtr mxcdMute, mxcd.paDetails, Len(mxcdMute)
    theExit:
    If hMem Then GlobalFree (hMem)
    If hMixer Then mixerClose (hMixer)
    
    If iErr <> 0 Then
    MsgBox "Couldn't read the Master Mute Control"
    Else
    GetMasterMuteState = CBool(mxcdMute.dwValue)
    End If
    End Function
    to a module and used
    Sub Ismuted()
    Call GetMasterMuteState
    End Sub
    to call the function but i get a series of compile errors stating
    'User defined type not defined' for the following

    Dim mxc As MIXERCONTROL
    Dim mxl As MIXERLINE
    Dim mxlc As MIXERLINECONTROLS
    Dim mxcd As MIXERCONTROLDETAILS
    Dim mxcdMute As MIXERCONTROLDETAILS_BOOLEAN

    do I need to add any additional 'References' to accept the MIXER types above

    Thanks for your time

  4. #4
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good morning Kerno

    No, you don't need need to check any references, but you do need all the additional code that comes before the actual function. Thiis states all the constants that are required and cals the APIs to find them. Here, with all due credit to KeepITCool, is the code from this thread :

    http://www.pcreview.co.uk/forums/thread-1008148.php

    Option Explicit
    
    Const MMSYSERR_NOERROR = 0
    Const MAXPNAMELEN = 32
    Const MIXER_LONG_NAME_CHARS = 64
    Const MIXER_SHORT_NAME_CHARS = 16
    
    Const MIXER_OBJECTF_HANDLE As Long = &H80000000
    Const MIXER_OBJECTF_MIXER As Long = &H0&
    Const MIXER_OBJECTF_HMIXER As Long = ( _
    MIXER_OBJECTF_HANDLE Or MIXER_OBJECTF_MIXER)
    
    Const MIXER_GETLINEINFOF_COMPONENTTYPE = &H3&
    Const MIXER_GETCONTROLDETAILSF_VALUE = &H0&
    Const MIXER_GETLINECONTROLSF_ONEBYTYPE = &H2&
    Const MIXERLINE_COMPONENTTYPE_DST_FIRST = &H0&
    Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS = ( _
    MIXERLINE_COMPONENTTYPE_DST_FIRST + 4)
    
    Const MIXERCONTROL_CT_CLASS_SWITCH As Long = &H20000000
    Const MIXERCONTROL_CT_SC_SWITCH_BOOLEAN As Long = &H0&
    Const MIXERCONTROL_CT_UNITS_BOOLEAN As Long = &H10000
    Const MIXERCONTROL_CONTROLTYPE_BOOLEAN As Long = ( _
    MIXERCONTROL_CT_CLASS_SWITCH Or _
    MIXERCONTROL_CT_SC_SWITCH_BOOLEAN Or _
    MIXERCONTROL_CT_UNITS_BOOLEAN)
    Const MIXERCONTROL_CONTROLTYPE_MUTE As Long = ( _
    MIXERCONTROL_CONTROLTYPE_BOOLEAN + 2)
    
    
    Declare Function mixerClose Lib "winmm.dll" ( _
    ByVal hmx As Long) As Long
    Declare Function mixerOpen Lib "winmm.dll" (phmx As Long, _
    ByVal uMxId As Long, ByVal dwCallback As Long, _
    ByVal dwInstance As Long, ByVal fdwOpen As Long) As Long
    
    Declare Function mixerGetControlDetails Lib "winmm.dll" _
    Alias "mixerGetControlDetailsA" (ByVal hmxobj As Long, _
    pmxcd As MIXERCONTROLDETAILS, _
    ByVal fdwDetails As Long) As Long
    Declare Function mixerGetLineControls Lib "winmm.dll" Alias _
    "mixerGetLineControlsA" (ByVal hmxobj As Long, _
    pmxlc As MIXERLINECONTROLS, _
    ByVal fdwControls As Long) As Long
    Declare Function mixerGetLineInfo Lib "winmm.dll" Alias _
    "mixerGetLineInfoA" (ByVal hmxobj As Long, _
    pmxl As MIXERLINE, ByVal fdwInfo As Long) As Long
    
    
    Declare Function GlobalAlloc Lib "kernel32" ( _
    ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Declare Function GlobalLock Lib "kernel32" ( _
    ByVal hMem As Long) As Long
    Declare Function GlobalFree Lib "kernel32" ( _
    ByVal hMem As Long) As Long
    Declare Sub CopyStructFromPtr Lib "kernel32" Alias _
    "RtlMoveMemory" (struct As Any, ByVal ptr As Long, _
    ByVal cb As Long)
    Declare Sub CopyPtrFromStruct Lib "kernel32" Alias _
    "RtlMoveMemory" (ByVal ptr As Long, struct As Any, _
    ByVal cb As Long)
    
    Type MIXERCONTROL
    cbStruct As Long
    dwControlID As Long
    dwControlType As Long
    fdwControl As Long
    cMultipleItems As Long
    szShortName As String * MIXER_SHORT_NAME_CHARS
    szName As String * MIXER_LONG_NAME_CHARS
    lMinimum As Long
    lMaximum As Long
    reserved(10) As Long
    End Type
    
    Type MIXERCONTROLDETAILS
    cbStruct As Long
    dwControlID As Long
    cChannels As Long
    item As Long
    cbDetails As Long
    paDetails As Long
    End Type
    
    Type MIXERCONTROLDETAILS_BOOLEAN
    dwValue As Long
    End Type
    
    Type MIXERLINE
    cbStruct As Long
    dwDestination As Long
    dwSource As Long
    dwLineID As Long
    fdwLine As Long
    dwUser As Long
    dwComponentType As Long
    cChannels As Long
    cConnections As Long
    cControls As Long
    szShortName As String * MIXER_SHORT_NAME_CHARS
    szName As String * MIXER_LONG_NAME_CHARS
    dwType As Long
    dwDeviceID As Long
    wMid As Integer
    wPid As Integer
    vDriverVersion As Long
    szPname As String * MAXPNAMELEN
    End Type
    
    Type MIXERLINECONTROLS
    cbStruct As Long
    dwLineID As Long
    dwControl As Long
    cControls As Long
    cbmxctrl As Long
    pamxctrl As Long
    End Type
    
    Function GetMasterMuteState() As Boolean
    ' This function reads the state of the masterMute control
    Dim hMixer As Long ' mixer handle
    
    Dim mxc As MIXERCONTROL
    Dim mxl As MIXERLINE
    Dim mxlc As MIXERLINECONTROLS
    Dim mxcd As MIXERCONTROLDETAILS
    Dim mxcdMute As MIXERCONTROLDETAILS_BOOLEAN
    
    Dim hMem As Long
    Dim rc As Long
    Dim iErr As Integer
    
    ' Open the mixer with deviceID 0.
    rc = mixerOpen(hMixer, 0, 0, 0, 0)
    If ((MMSYSERR_NOERROR <> rc)) Then iErr = 1: GoTo theExit
    
    mxl.cbStruct = Len(mxl)
    mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
    
    ' Obtain a line corresponding to the component type
    rc = mixerGetLineInfo(hMixer, mxl, _
    MIXER_GETLINEINFOF_COMPONENTTYPE)
    If (MMSYSERR_NOERROR <> rc) Then iErr = 2: GoTo theExit
    
    
    mxlc.cbStruct = Len(mxlc)
    mxlc.dwLineID = mxl.dwLineID
    mxlc.dwControl = MIXERCONTROL_CONTROLTYPE_MUTE
    mxlc.cControls = 1
    mxlc.cbmxctrl = Len(mxc)
    
    ' Allocate a buffer for the control
    hMem = GlobalAlloc(&H40, Len(mxc))
    mxlc.pamxctrl = GlobalLock(hMem)
    mxc.cbStruct = Len(mxc)
    
    ' Get the control
    rc = mixerGetLineControls(hMixer, mxlc, _
    MIXER_GETLINECONTROLSF_ONEBYTYPE)
    If (MMSYSERR_NOERROR <> rc) Then iErr = 3: GoTo theExit
    'Copy into mxc structure
    CopyStructFromPtr mxc, mxlc.pamxctrl, Len(mxc)
    GlobalFree (hMem)
    hMem = 0
    
    'Get the controldetails
    mxcd.cbStruct = Len(mxcd)
    mxcd.dwControlID = mxc.dwControlID
    mxcd.cChannels = 1
    mxcd.item = 0
    mxcd.cbDetails = Len(mxcdMute)
    
    ' Allocate a buffer for the controldetails
    hMem = GlobalAlloc(&H40, Len(mxcdMute))
    mxcd.paDetails = GlobalLock(hMem)
    
    'Get the controldetailvalue
    rc = mixerGetControlDetails(hMixer, mxcd, _
    MIXER_OBJECTF_HMIXER Or MIXER_GETCONTROLDETAILSF_VALUE)
    If (MMSYSERR_NOERROR <> rc) Then iErr = 4: GoTo theExit
    ' Copy into mxcdMute structure
    CopyStructFromPtr mxcdMute, mxcd.paDetails, Len(mxcdMute)
    theExit:
    If hMem Then GlobalFree (hMem)
    If hMixer Then mixerClose (hMixer)
    
    If iErr <> 0 Then
    MsgBox "Couldn't read the Master Mute Control"
    Else
    GetMasterMuteState = CBool(mxcdMute.dwValue)
    End If
    End Function
    Use this to call it from VBA, which will return the boolean value of TRUE / FALSE.

    Sub test()
    MsgBox GetMasterMuteState()
    End Sub
    HTH

    DominicB

  5. #5
    Registered User
    Join Date
    07-03-2007
    Location
    Canberra, Australia
    MS-Off Ver
    2010
    Posts
    60
    Hi dominicb,

    Thanks for your reply the code works great, again thanks for your help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1