CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: How switch on/off sound?

  1. #1
    Join Date
    Jun 2001
    Posts
    28

    How switch on/off sound?

    Hi all,

    I wish to switch on/off the sound programmatically. How can I do this?

    Best regards

    Robert

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332
    One way might be to set the master volume to 0. Just remember to save the original setting so you can restore the level to where it was before. There are API calls you can use for this.

  3. #3
    Join Date
    Jun 2001
    Posts
    28

    Question

    Thanks for the reply.
    I haven't done this before. Could you give me a hint, what API's to use?

    Regards

    Robert

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Example from api-guide and info from msdn

    From MSDN:
    'auxSetVolume
    'The auxSetVolume function sets the volume of the specified
    auxiliary output device.
    '
    'MMRESULT auxSetVolume(
    ' UINT uDeviceID,
    ' DWORD dwVolume
    ');
    'Parameters
    'uDeviceID
    'Identifier of the auxiliary output device to be queried. Device
    identifiers are determined implicitly from the number of devices
    present in the system. Device identifier values range from zero to
    one less than the number of devices present. Use the
    auxGetNumDevs function to determine the number of auxiliary
    devices in the system.
    'dwVolume
    'Specifies the new volume setting. The low-order word specifies
    the left-channel volume setting, and the high-order word
    specifies the right-channel setting. A value of 0xFFFF represents
    full volume, and a value of 0x0000 is silence.
    'If a device does not support both left and right volume control,
    the low-order word of dwVolume specifies the volume level, and
    the high-order word is ignored.
    'Return Values
    'Returns MMSYSERR_NOERROR if successful or an error otherwise.
    Possible error values include the following.
    'Value Description
    'MMSYSERR_BADDEVICEID Specified device identifier is out of range.
    'Remarks
    'Not all devices support volume control. To determine whether the
    device supports volume control, use the AUXCAPS_VOLUME flag
    to test the dwSupport member of the AUXCAPS structure (filled by
    the auxGetDevCaps function).
    'To determine whether the device supports volume control on
    both the left and right channels, use the AUXCAPS_LRVOLUME
    flag to test the dwSupport member of the AUXCAPS structure
    (filled by auxGetDevCaps).
    'Most devices do not support the full 16 bits of volume-level
    control and will use only the high-order bits of the requested
    volume setting. For example, for a device that supports 4 bits of
    volume control, requested volume level values of 0x4000, 0x4FFF,
    and 0x43BE will produce the same physical volume setting,
    0x4000. The auxGetVolume function will return the full 16-bit
    setting set with auxSetVolume.
    'Volume settings are interpreted logarithmically. This means the
    perceived volume increase is the same when increasing the
    volume level from 0x5000 to 0x6000 as it is from 0x4000 to
    0x5000.
    'Requirements
    ' Windows NT/2000/XP: Included in Windows NT 3.1 and later.
    ' Windows 95/98/Me: Included in Windows 95 and later.
    ' Header: Declared in Mmsystem.h; include Windows
    'Example from Api -Guide---------------------------
    Code:
    Option Explicit
    Private Const HIGHEST_VOLUME_SETTING = 100 '%
    Private Const AUX_MAPPER = -1&
    Private Const MAXPNAMELEN = 32
    Private Const AUXCAPS_CDAUDIO = 1  ' audio from internal CD-ROM drive
    Private Const AUXCAPS_AUXIN = 2  ' audio from auxiliary input jacks
    Private Const AUXCAPS_VOLUME = &H1         ' supports volume control
    Private Const AUXCAPS_LRVOLUME = &H2         ' separate left-right volume control
    Private Const MMSYSERR_NOERROR = 0
    Private Const MMSYSERR_BASE = 0
    Private Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2)
    Private Type AUXCAPS
           wMid As Integer
           wPid As Integer
           vDriverVersion As Long
           szPname As String * MAXPNAMELEN
           wTechnology As Integer
           dwSupport As Long
    End Type
    Private Type VolumeSetting
        LeftVol As Integer
        RightVol As Integer
    End Type
    Private Declare Function auxGetNumDevs Lib "winmm.dll" () As Long
    Private Declare Function auxGetDevCaps Lib "winmm.dll" Alias "auxGetDevCapsA" (ByVal uDeviceID As Long, lpCaps As AUXCAPS, ByVal uSize As Long) As Long
    Private Declare Function auxSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long
    Private Declare Function auxGetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByRef lpdwVolume As VolumeSetting) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    Private Function nSigned(ByVal lUnsignedInt As Long) As Integer
        Dim nReturnVal As Integer                         ' Return value from Function
        If lUnsignedInt > 65535 Or lUnsignedInt < 0 Then
            MsgBox "Error in conversion from Unsigned to nSigned Integer"
            nSignedInt = 0
            Exit Function
        End If
        If lUnsignedInt > 32767 Then
            nReturnVal = lUnsignedInt - 65536
        Else
            nReturnVal = lUnsignedInt
        End If
        nSigned = nReturnVal
    End Function
    Private Function lUnsigned(ByVal nSignedInt As Integer) As Long
        Dim lReturnVal As Long                         ' Return value from Function
        If nSignedInt < 0 Then
            lReturnVal = nSignedInt + 65536
        Else
            lReturnVal = nSignedInt
        End If
        If lReturnVal > 65535 Or lReturnVal < 0 Then
            MsgBox "Error in conversion from nSigned to Unsigned Integer"
            lReturnVal = 0
        End If
        lUnsigned = lReturnVal
    End Function
    Private Function lSetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long
        Dim Volume As VolumeSetting, lBothVolumes As Long
        Volume.LeftVol = nSigned(lLeftVol * 65535 / HIGHEST_VOLUME_SETTING)
        Volume.RightVol = nSigned(lRightVol * 65535 / HIGHEST_VOLUME_SETTING)
        'copy our Volume-variable to a long
        CopyMemory lBothVolumes, Volume.LeftVol, Len(Volume)
        'call the SetVolume-function
        lSetVolume = auxSetVolume(lDeviceID, lBothVolumes)
    End Function
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@Allapi.net
        Dim Volume As VolumeSetting, Cnt As Long, AC As AUXCAPS
        'set the output to a persistent graphic
        Me.AutoRedraw = True
        'loop through all the devices
        For Cnt = 0 To auxGetNumDevs - 1 'auxGetNumDevs is zero-based
            'get the volume
            auxGetVolume Cnt, Volume
            'get the device capabilities
            auxGetDevCaps Cnt, AC, Len(AC)
            'print the name on the form
            Me.Print "Device #" + Str$(Cnt + 1) + ": " + Left(AC.szPname, InStr(AC.szPname, vbNullChar) - 1)
            'print the left- and right volume on the form
            Me.Print "Left volume:" + Str$(HIGHEST_VOLUME_SETTING * lUnsigned(Volume.LeftVol) / 65535)
            Me.Print "Right volume:" + Str$(HIGHEST_VOLUME_SETTING * lUnsigned(Volume.RightVol) / 65535)
            'set the left- and right-volume to 50%
            lSetVolume 50, 50, Cnt
            Me.Print "Both volumes now set to 50%"
            'empty line
            Me.Print
        Next
    End Sub
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jun 2001
    Posts
    28

    Question

    Thanks for the code, I haven't tried out it yet.
    However, to be more precise, I want to only control the volume of the PC sound. How can I identify the corresponding device?

    Thanks

    Robert

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    If you cannot debug...

    ...I have no soundcard here, thus cannot tell you. But I found these at planet source code. Have a look:
    http://planet-source-code.com/vb/scr...42946&lngWId=1
    http://planet-source-code.com/vb/scr...38589&lngWId=1
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)