Systemweite Lautstärke

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von NscMike.

    Systemweite Lautstärke

    Hallo leute, ich möchte die Systemweite Lautstärke per Code verändern können. Es gab auch mal eine Libary ([Release] SysVolume) aber leider gibt es die die Datei nicht mehr. Kann jemand helfen? MfG
    Das ist nicht so einfach ich habe damit auch meine Probleme.
    Vielleicht Hilft dir mein PRoblempost weiter:

    [Allgemein] Volumen ändern funktioniert nicht O.o
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Hi

    Sieh dir mal das hier an:

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class Sound
    3. #Region "Comments"
    4. ' This class is responisble for all interactions for muting and controlling the master volume level
    5. ' There is no warranty associated with this code - if you use this and it blows up you machine.
    6. ' Well, that will teach you to write you own code in future, won't it ;-)
    7. ' Mark Dryden (aka Drydo@vbcity.com)
    8. #End Region
    9. #Region "Constants"
    10. Private Const MMSYSERR_NOERROR As Integer = 0
    11. Private Const MAXPNAMELEN As Integer = 32
    12. Private Const MIXER_LONG_NAME_CHARS As Integer = 64
    13. Private Const MIXER_SHORT_NAME_CHARS As Integer = 16
    14. Private Const MIXERCONTROL_CT_CLASS_FADER As Integer = &H50000000
    15. Private Const MIXERCONTROL_CT_UNITS_UNSIGNED As Integer = &H30000
    16. Private Const MIXERCONTROL_CT_UNITS_BOOLEAN As Integer = &H10000
    17. Private Const MIXERCONTROL_CT_CLASS_SWITCH As Integer = &H20000000
    18. Private Const MIXERLINE_COMPONENTTYPE_DST_FIRST As Integer = &H0&
    19. Private Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS As Integer = (MIXERLINE_COMPONENTTYPE_DST_FIRST + 4)
    20. Private Const MIXERCONTROL_CONTROLTYPE_FADER As Integer = (MIXERCONTROL_CT_CLASS_FADER Or MIXERCONTROL_CT_UNITS_UNSIGNED)
    21. Private Const MIXERCONTROL_CONTROLTYPE_VOLUME As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 1)
    22. Private Const MIXER_GETLINEINFOF_COMPONENTTYPE As Integer = &H3&
    23. Private Const MIXER_GETLINECONTROLSF_ONEBYTYPE As Integer = &H2
    24. Private Const MIXERCONTROL_CONTROLTYPE_BASS As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 2)
    25. Private Const MIXERCONTROL_CONTROLTYPE_TREBLE As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 3)
    26. Private Const MIXERCONTROL_CONTROLTYPE_EQUALIZER As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 4)
    27. Private Const MIXERCONTROL_CONTROLTYPE_BOOLEAN As Integer = (MIXERCONTROL_CT_CLASS_SWITCH Or MIXERCONTROL_CT_UNITS_BOOLEAN)
    28. Private Const MIXERCONTROL_CONTROLTYPE_MUTE As Integer = (MIXERCONTROL_CONTROLTYPE_BOOLEAN + 2)
    29. #End Region
    30. #Region "Structs"
    31. <StructLayout(LayoutKind.Sequential)> _
    32. Private Structure MIXERCONTROL
    33. <FieldOffset(0)> Public cbStruct As Integer ' size in Byte of MIXERCONTROL
    34. <FieldOffset(4)> Public dwControlID As Integer ' unique control id for mixer device
    35. <FieldOffset(8)> Public dwControlType As Integer ' MIXERCONTROL_CONTROLTYPE_xxx
    36. <FieldOffset(12)> Public fdwControl As Integer ' MIXERCONTROL_CONTROLF_xxx
    37. <FieldOffset(16)> Public cMultipleItems As Integer ' if MIXERCONTROL_CONTROLF_MULTIPLE set
    38. <FieldOffset(20), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String ' * MIXER_SHORT_NAME_CHARS ' short name of control
    39. <FieldOffset(36), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String ' * MIXER_LONG_NAME_CHARS ' Integer name of control
    40. <FieldOffset(100)> Public lMinimum As Integer ' Minimum value
    41. <FieldOffset(104)> Public lMaximum As Integer ' Maximum value
    42. <FieldOffset(108), MarshalAs(UnmanagedType.ByValArray, SizeConst:=11, ArraySubType:=UnmanagedType.AsAny)> Public reserved() As Integer ' reserved structure space
    43. End Structure
    44. <StructLayout(LayoutKind.Sequential)> _
    45. Private Structure MIXERCONTROLDETAILS
    46. <FieldOffset(0)> Public cbStruct As Integer ' size in Byte of MIXERCONTROLDETAILS
    47. <FieldOffset(4)> Public dwControlID As Integer ' control id to get/set details on
    48. <FieldOffset(8)> Public cChannels As Integer ' number of channels in paDetails array
    49. <FieldOffset(12)> Public item As Integer ' hwndOwner or cMultipleItems
    50. <FieldOffset(16)> Public cbDetails As Integer ' size of _one_ details_XX struct
    51. <FieldOffset(20)> Public paDetails As IntPtr ' pointer to array of details_XX structs
    52. End Structure
    53. <StructLayout(LayoutKind.Sequential)> _
    54. Private Structure MIXERCONTROLDETAILS_UNSIGNED
    55. <FieldOffset(0)> Public dwValue As Integer ' value of the control
    56. End Structure
    57. <StructLayout(LayoutKind.Sequential)> _
    58. Private Structure MIXERLINECONTROLS
    59. <FieldOffset(0)> Public cbStruct As Integer ' size in Byte of MIXERLINECONTROLS
    60. <FieldOffset(4)> Public dwLineID As Integer ' line id (from MIXERLINE.dwLineID)
    61. <FieldOffset(8)> Public dwControl As Integer ' MIXER_GETLINECONTROLSF_ONEBYTYPE
    62. <FieldOffset(12)> Public cControls As Integer ' count of controls pmxctrl points to
    63. <FieldOffset(16)> Public cbmxctrl As Integer ' size in Byte of _one_ MIXERCONTROL
    64. <FieldOffset(20)> Public pamxctrl As IntPtr ' pointer to first MIXERCONTROL array
    65. End Structure
    66. <StructLayout(LayoutKind.Sequential)> _
    67. Private Structure MIXERLINE
    68. <FieldOffset(0)> Public cbStruct As Integer ' size of MIXERLINE structure
    69. <FieldOffset(4)> Public dwDestination As Integer ' zero based destination index
    70. <FieldOffset(8)> Public dwSource As Integer ' zero based source index (if source)
    71. <FieldOffset(12)> Public dwLineID As Integer ' unique line id for mixer device
    72. <FieldOffset(16)> Public fdwLine As Integer ' state/information about line
    73. <FieldOffset(20)> Public dwUser As Integer ' driver specific information
    74. <FieldOffset(24)> Public dwComponentType As Integer ' component type line connects to
    75. <FieldOffset(28)> Public cChannels As Integer ' number of channels line supports
    76. <FieldOffset(32)> Public cConnections As Integer ' number of connections (possible)
    77. <FieldOffset(36)> Public cControls As Integer ' number of controls at this line
    78. <FieldOffset(40), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String ' * MIXER_SHORT_NAME_CHARS
    79. <FieldOffset(56), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String ' * MIXER_LONG_NAME_CHARS
    80. <FieldOffset(120)> Public dwType As Integer
    81. <FieldOffset(124)> Public dwDeviceID As Integer
    82. <FieldOffset(128)> Public wMid As Integer
    83. <FieldOffset(132)> Public wPid As Integer
    84. <FieldOffset(136)> Public vDriverVersion As Integer
    85. <FieldOffset(168), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String ' * MAXPNAMELEN
    86. End Structure
    87. #End Region
    88. #Region "API Declarations"
    89. Private Declare Function mixerOpen Lib "winmm.dll" _
    90. (ByRef phmx As Integer, <MarshalAs(UnmanagedType.U4)> ByVal uMxId As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal fdwOpen As Integer) As Integer
    91. Private Declare Function mixerGetLineInfo Lib "winmm.dll" Alias "mixerGetLineInfoA" _
    92. (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As Integer) As Integer
    93. Private Declare Function mixerGetLineControls Lib "winmm.dll" Alias "mixerGetLineControlsA" _
    94. (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxlc As MIXERLINECONTROLS, ByVal fdwControls As Integer) As Integer
    95. Private Declare Function mixerSetControlDetails Lib "winmm.dll" (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, _
    96. ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
    97. #End Region
    98. Public Sub SetVolume(ByVal Level As Integer)
    99. ' Sets the volume to a specific percentage as passed through
    100. Dim hmixer As Integer
    101. Dim volCtrl As New MIXERCONTROL
    102. Dim lngReturn As Integer
    103. Dim lngVolSetting As Integer
    104. ' Obtain the hmixer struct
    105. lngReturn = mixerOpen(hmixer, 0, 0, 0, 0)
    106. ' Error check
    107. If lngReturn <> 0 Then Exit Sub
    108. ' Obtain the volumne control object
    109. Call GetVolumeControl(hmixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, _
    110. MIXERCONTROL_CONTROLTYPE_VOLUME, volCtrl)
    111. ' Then determine the value of the volume
    112. lngVolSetting = CType(volCtrl.lMaximum * (Level / 100), Integer)
    113. ' Then set the volume
    114. SetVolumeControl(hmixer, volCtrl, lngVolSetting)
    115. End Sub
    116. Public Sub SetSound(ByVal boolMute As Boolean)
    117. ' This routine sets the volume setting of the current unit depending on the value passed through
    118. Dim hmixer As Integer
    119. Dim volCtrl As New MIXERCONTROL
    120. Dim lngReturn As Integer
    121. Dim lngVolSetting As Integer
    122. ' Obtain the hmixer struct
    123. lngReturn = mixerOpen(hmixer, 0, 0, 0, 0)
    124. ' Error check
    125. If lngReturn <> 0 Then Exit Sub
    126. ' Obtain the volumne control object
    127. Call GetVolumeControl(hmixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, _
    128. MIXERCONTROL_CONTROLTYPE_MUTE, volCtrl)
    129. ' Then determine the value of the volume
    130. If boolMute Then
    131. ' Mute
    132. lngVolSetting = 1
    133. Else
    134. ' Turn the sound on
    135. lngVolSetting = 0
    136. End If
    137. ' Then set the volume
    138. SetVolumeControl(hmixer, volCtrl, lngVolSetting)
    139. End Sub
    140. Private Function GetVolumeControl(ByVal hmixer As Integer, ByVal componentType As Integer, ByVal ctrlType As Integer, _
    141. ByRef mxc As MIXERCONTROL) As Boolean
    142. ' Obtains an appropriate pointer and info for the volume control
    143. ' [Note: original source taken from MSDN http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q178456&]
    144. ' This function attempts to obtain a mixer control. Returns True if successful.
    145. Dim mxlc As New MIXERLINECONTROLS
    146. Dim mxl As New MIXERLINE
    147. Dim rc As Integer, pmem As IntPtr
    148. mxl.cbStruct = Marshal.SizeOf(mxl)
    149. mxl.dwComponentType = componentType
    150. ' Obtain a line corresponding to the component type
    151. rc = mixerGetLineInfo(hmixer, mxl, MIXER_GETLINEINFOF_COMPONENTTYPE)
    152. If (MMSYSERR_NOERROR = rc) Then
    153. mxlc.cbStruct = Marshal.SizeOf(mxlc)
    154. mxlc.dwLineID = mxl.dwLineID
    155. mxlc.dwControl = ctrlType
    156. mxlc.cControls = 1
    157. mxlc.cbmxctrl = Marshal.SizeOf(mxc)
    158. ' Allocate a buffer for the control
    159. pmem = Marshal.AllocHGlobal(Marshal.SizeOf(mxc))
    160. mxlc.pamxctrl = pmem
    161. mxc.cbStruct = Marshal.SizeOf(mxc)
    162. ' Get the control
    163. rc = mixerGetLineControls(hmixer, _
    164. mxlc, _
    165. MIXER_GETLINECONTROLSF_ONEBYTYPE)
    166. If (MMSYSERR_NOERROR = rc) Then
    167. mxc = CType(Marshal.PtrToStructure(mxlc.pamxctrl, GetType(MIXERCONTROL)), MIXERCONTROL)
    168. Marshal.FreeHGlobal(pmem)
    169. Return True
    170. End If
    171. Marshal.FreeHGlobal(pmem)
    172. Exit Function
    173. End If
    174. Return False
    175. End Function
    176. Private Function SetVolumeControl(ByVal hmixer As Integer, _
    177. ByVal mxc As MIXERCONTROL, _
    178. ByVal volume As Integer) As Boolean
    179. ' Sets the volumne from the pointer of the object passed through
    180. ' [Note: original source taken from MSDN http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q178456&]
    181. 'This function sets the value for a volume control. Returns True if successful
    182. Dim mxcd As MIXERCONTROLDETAILS
    183. Dim vol As MIXERCONTROLDETAILS_UNSIGNED
    184. Dim rc As Integer
    185. Dim hptr As IntPtr
    186. mxcd.item = 0
    187. mxcd.dwControlID = mxc.dwControlID
    188. mxcd.cbStruct = Marshal.SizeOf(mxcd)
    189. mxcd.cbDetails = Marshal.SizeOf(vol)
    190. hptr = Marshal.AllocHGlobal(Marshal.SizeOf(vol))
    191. ' Allocate a buffer for the control value buffer
    192. mxcd.paDetails = hptr
    193. mxcd.cChannels = 1
    194. vol.dwValue = volume
    195. Marshal.StructureToPtr(vol, hptr, False)
    196. ' Set the control value
    197. rc = mixerSetControlDetails(hmixer, _
    198. mxcd, _
    199. 0)
    200. Marshal.FreeHGlobal(hptr)
    201. If (MMSYSERR_NOERROR = rc) Then
    202. Return True
    203. Else
    204. Return False
    205. End If
    206. End Function
    207. End Class




    Der Code stammt zwar nicht von mir, sollte aber funktionieren. Fragt mich bitte nicht wo ich den her habe... Ich weis es nicht mehr