Aktuelle Lautstärkeeinstellung von Windows auslesen

  • VB.NET

Es gibt 16 Antworten in diesem Thema. Der letzte Beitrag () ist von pc-freack.

    Aktuelle Lautstärkeeinstellung von Windows auslesen

    Glaubt mir, ich hab die SuFu verwendet.
    Dieses Thema wurde schon oft behandelt, aber bisher hat bei mir nichts gefunzt.
    Die GetVolume() Funktion der SysVolume.dll gibt bei mir immer den Wert 10 zurück.
    Die Bass.dll funktioniert bei mir aus irgendeinem Grund nicht.

    Die genaue Problembeschreibung:

    Die Lautstärke, die im Mixer eingestellt ist, nicht der Pegel sollte in einem Label angezeigt werden (0 bedeutet Minimum, 100 bedeutet Maximum).

    Das Programm selbst hab ich bereits fertig, nur dass eben die GetVolume() Funktion immer 10 zurückgibt. Darum funktioniert es noch nicht.


    Ich bitte euch noch:
    Postet bitte keine Links. Das führt dann wieder dazu, dass ich kreuz und quer durch Foren verwießen werde.
    Andererseits sind C&P Codes auch nicht nötig ;)
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Niko Ortner“ ()

    ich hab das gleiche Problem.
    Bei mir wird auch immer nur 10 ausgegeben.

    Hier ist der ganze code:

    Code

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.Runtime.InteropServices
    3. Public Class Form1
    4. Private S As New AudioMixerHelper
    5. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6. Timer1.Start()
    7. End Sub
    8. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    9. Label1.Text = "Aktuelle Lautstärke: " & S.GetVolume
    10. End Sub
    11. End Class
    12. Public Class AudioMixerHelper
    13. Private Const MMSYSERR_NOERROR As Integer = 0
    14. Private Const MAXPNAMELEN As Integer = 32
    15. Private Const MIXER_LONG_NAME_CHARS As Integer = 64
    16. Private Const MIXER_SHORT_NAME_CHARS As Integer = 16
    17. Private Const MIXER_GETLINEINFOF_COMPONENTTYPE As Integer = &H3
    18. Private Const MIXER_GETCONTROLDETAILSF_VALUE As Integer = &H0
    19. Private Const MIXER_GETLINECONTROLSF_ONEBYTYPE As Integer = &H2
    20. Private Const MIXER_SETCONTROLDETAILSF_VALUE As Integer = &H0
    21. Private Const MIXERLINE_COMPONENTTYPE_DST_FIRST As Integer = &H0
    22. Private Const MIXERLINE_COMPONENTTYPE_SRC_FIRST As Integer = &H1000
    23. Private Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS As Integer = MIXERLINE_COMPONENTTYPE_DST_FIRST + 4
    24. Private Const MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE As Integer = MIXERLINE_COMPONENTTYPE_SRC_FIRST + 3
    25. Private Const MIXERLINE_COMPONENTTYPE_SRC_LINE As Integer = MIXERLINE_COMPONENTTYPE_SRC_FIRST + 2
    26. Private Const MIXERCONTROL_CT_CLASS_FADER As Integer = &H50000000
    27. Private Const MIXERCONTROL_CT_UNITS_UNSIGNED As Integer = &H30000
    28. Private Const MIXERCONTROL_CONTROLTYPE_FADER As Integer = MIXERCONTROL_CT_CLASS_FADER Or MIXERCONTROL_CT_UNITS_UNSIGNED
    29. Private Const MIXERCONTROL_CONTROLTYPE_VOLUME As Integer = MIXERCONTROL_CONTROLTYPE_FADER + 1
    30. Private Declare Ansi Function mixerClose Lib "winmm.dll" (ByVal hmx As Integer) As Integer
    31. Private Declare Ansi Function mixerGetControlDetailsA Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
    32. Private Declare Ansi Function mixerGetDevCapsA Lib "winmm.dll" (ByVal uMxId As Integer, ByVal pmxcaps As MIXERCAPS, ByVal cbmxcaps As Integer) As Integer
    33. Private Declare Ansi Function mixerGetID Lib "winmm.dll" (ByVal hmxobj As Integer, ByVal pumxID As Integer, ByVal fdwId As Integer) As Integer
    34. Private Declare Ansi Function mixerGetLineControlsA Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxlc As MIXERLINECONTROLS, ByVal fdwControls As Integer) As Integer
    35. Private Declare Ansi Function mixerGetLineInfoA Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As Integer) As Integer
    36. Private Declare Ansi Function mixerGetNumDevs Lib "winmm.dll" () As Integer
    37. Private Declare Ansi Function mixerMessage Lib "winmm.dll" (ByVal hmx As Integer, ByVal uMsg As Integer, ByVal dwParam1 As Integer, ByVal dwParam2 As Integer) As Integer
    38. Private Declare Ansi Function mixerOpen Lib "winmm.dll" (ByRef phmx As Integer, ByVal uMxId As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal fdwOpen As Integer) As Integer
    39. Private Declare Ansi Function mixerSetControlDetails Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
    40. Private Structure MIXERCAPS
    41. Public wMid As Integer
    42. Public wPid As Integer
    43. Public vDriverVersion As Integer
    44. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String
    45. Public fdwSupport As Integer
    46. Public cDestinations As Integer
    47. End Structure 'MIXERCAPS
    48. Private Structure MIXERCONTROL
    49. Public cbStruct As Integer
    50. Public dwControlID As Integer
    51. Public dwControlType As Integer
    52. Public fdwControl As Integer
    53. Public cMultipleItems As Integer
    54. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String
    55. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String
    56. Public lMinimum As Integer
    57. Public lMaximum As Integer
    58. <MarshalAs(UnmanagedType.U4, SizeConst:=10)> Public reserved As Integer
    59. End Structure 'MIXERCONTROL
    60. Private Structure MIXERCONTROLDETAILS
    61. Public cbStruct As Integer
    62. Public dwControlID As Integer
    63. Public cChannels As Integer
    64. Public item As Integer
    65. Public cbDetails As Integer
    66. Public paDetails As IntPtr
    67. End Structure 'MIXERCONTROLDETAILS
    68. Private Structure MIXERCONTROLDETAILS_UNSIGNED
    69. Public dwValue As Integer
    70. End Structure 'MIXERCONTROLDETAILS_UNSIGNED
    71. Private Structure MIXERLINE
    72. Public cbStruct As Integer
    73. Public dwDestination As Integer
    74. Public dwSource As Integer
    75. Public dwLineID As Integer
    76. Public fdwLine As Integer
    77. Public dwUser As Integer
    78. Public dwComponentType As Integer
    79. Public cChannels As Integer
    80. Public cConnections As Integer
    81. Public cControls As Integer
    82. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String
    83. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String
    84. Public dwType As Integer
    85. Public dwDeviceID As Integer
    86. Public wMid As Integer
    87. Public wPid As Integer
    88. Public vDriverVersion As Integer
    89. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String
    90. End Structure 'MIXERLINE
    91. Private Structure MIXERLINECONTROLS
    92. Public cbStruct As Integer
    93. Public dwLineID As Integer
    94. Public dwControl As Integer
    95. Public cControls As Integer
    96. Public cbmxctrl As Integer
    97. Public pamxctrl As IntPtr
    98. End Structure 'MIXERLINECONTROLS
    99. Private Function GetVolumeControl(ByVal hmixer As Integer, ByVal componentType As Integer, ByVal ctrlType As Integer, ByRef mxc As MIXERCONTROL, ByRef vCurrentVol As Integer) As Boolean
    100. ' This function attempts to obtain a mixer control.
    101. ' Returns True if successful.
    102. Dim mxlc As New MIXERLINECONTROLS
    103. Dim mxl As New MIXERLINE
    104. Dim pmxcd As New MIXERCONTROLDETAILS
    105. Dim du As New MIXERCONTROLDETAILS_UNSIGNED
    106. mxc = New MIXERCONTROL
    107. Dim rc As Integer
    108. Dim retValue As Boolean
    109. vCurrentVol = -1
    110. mxl.cbStruct = Marshal.SizeOf(mxl)
    111. mxl.dwComponentType = componentType
    112. rc = mixerGetLineInfoA(hmixer, mxl, MIXER_GETLINEINFOF_COMPONENTTYPE)
    113. If MMSYSERR_NOERROR = rc Then
    114. Dim sizeofMIXERCONTROL As Integer = 152
    115. Dim ctrl As Integer = Marshal.SizeOf(GetType(MIXERCONTROL))
    116. mxlc.pamxctrl = Marshal.AllocCoTaskMem(sizeofMIXERCONTROL)
    117. mxlc.cbStruct = Marshal.SizeOf(mxlc)
    118. mxlc.dwLineID = mxl.dwLineID
    119. mxlc.dwControl = ctrlType
    120. mxlc.cControls = 1
    121. mxlc.cbmxctrl = sizeofMIXERCONTROL
    122. ' Allocate a buffer for the control
    123. mxc.cbStruct = sizeofMIXERCONTROL
    124. ' Get the control
    125. rc = mixerGetLineControlsA(hmixer, mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE)
    126. If MMSYSERR_NOERROR = rc Then
    127. retValue = True
    128. ' Copy the control into the destination structure
    129. mxc = CType(Marshal.PtrToStructure(mxlc.pamxctrl, GetType(MIXERCONTROL)), MIXERCONTROL)
    130. Else
    131. retValue = False
    132. End If
    133. Dim sizeofMIXERCONTROLDETAILS As Integer = Marshal.SizeOf(GetType(MIXERCONTROLDETAILS))
    134. Dim sizeofMIXERCONTROLDETAILS_UNSIGNED As Integer = Marshal.SizeOf(GetType(MIXERCONTROLDETAILS_UNSIGNED))
    135. pmxcd.cbStruct = sizeofMIXERCONTROLDETAILS
    136. pmxcd.dwControlID = mxc.dwControlID
    137. pmxcd.paDetails = Marshal.AllocCoTaskMem(sizeofMIXERCONTROLDETAILS_UNSIGNED)
    138. pmxcd.cChannels = 1
    139. pmxcd.item = 0
    140. pmxcd.cbDetails = sizeofMIXERCONTROLDETAILS_UNSIGNED
    141. rc = mixerGetControlDetailsA(hmixer, pmxcd, MIXER_GETCONTROLDETAILSF_VALUE)
    142. du = CType(Marshal.PtrToStructure(pmxcd.paDetails, GetType(MIXERCONTROLDETAILS_UNSIGNED)), Global.SoundDevice.AudioMixerHelper.MIXERCONTROLDETAILS_UNSIGNED)
    143. vCurrentVol = du.dwValue
    144. Return retValue
    145. End If
    146. retValue = False
    147. Return retValue
    148. End Function 'GetVolumeControl
    149. Private Function SetVolumeControl(ByVal hmixer As Integer, ByVal mxc As MIXERCONTROL, ByVal volume As Integer) As Boolean
    150. ' This function sets the value for a volume control.
    151. ' Returns True if successful
    152. Dim retValue As Boolean
    153. Dim rc As Integer
    154. Dim mxcd As New MIXERCONTROLDETAILS
    155. Dim vol As New MIXERCONTROLDETAILS_UNSIGNED
    156. mxcd.item = 0
    157. mxcd.dwControlID = mxc.dwControlID
    158. mxcd.cbStruct = Marshal.SizeOf(mxcd)
    159. mxcd.cbDetails = Marshal.SizeOf(vol)
    160. ' Allocate a buffer for the control value buffer
    161. mxcd.cChannels = 1
    162. vol.dwValue = volume
    163. ' Copy the data into the control value buffer
    164. mxcd.paDetails = Marshal.AllocCoTaskMem(Marshal.SizeOf(GetType(MIXERCONTROLDETAILS_UNSIGNED)))
    165. Marshal.StructureToPtr(vol, mxcd.paDetails, False)
    166. ' Set the control value
    167. rc = mixerSetControlDetails(hmixer, mxcd, MIXER_SETCONTROLDETAILSF_VALUE)
    168. If MMSYSERR_NOERROR = rc Then
    169. retValue = True
    170. Else
    171. retValue = False
    172. End If
    173. Return retValue
    174. End Function 'SetVolumeControl
    175. Public Function GetVolume() As Integer
    176. Dim mixer As Integer
    177. Dim volCtrl As New MIXERCONTROL
    178. Dim currentVol As Integer
    179. Dim type As Integer = MIXERCONTROL_CONTROLTYPE_VOLUME
    180. mixerOpen(mixer, 0, 0, 0, 0)
    181. GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, Type, volCtrl, currentVol)
    182. mixerClose(mixer)
    183. Return CInt(10 / (65535 / currentVol))
    184. End Function 'GetVolume
    185. Public Sub SetVolume(ByVal vVolume As Integer)
    186. Dim mixer As Integer
    187. Dim volCtrl As New MIXERCONTROL
    188. Dim currentVol As Integer
    189. Dim type As Integer = MIXERCONTROL_CONTROLTYPE_VOLUME
    190. mixerOpen(mixer, 0, 0, 0, 0)
    191. GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, volCtrl, currentVol)
    192. If vVolume > volCtrl.lMaximum Then
    193. vVolume = volCtrl.lMaximum
    194. End If
    195. If vVolume < volCtrl.lMinimum Then
    196. vVolume = volCtrl.lMinimum
    197. End If
    198. Dim L As Integer = CInt(65535 * vVolume / 10)
    199. SetVolumeControl(mixer, volCtrl, L)
    200. GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, volCtrl, currentVol)
    201. If L <> currentVol Then
    202. Throw New Exception("Cannot Set Volume")
    203. End If
    204. mixerClose(mixer)
    205. End Sub 'SetVolume
    206. End Class 'AudioMixerHelper




    Ich hoffe ihr könnt mir helfen

    VB.NET-Quellcode

    1. Public Function GetVolume() As Integer
    2. Dim mixer As Integer
    3. Dim volCtrl As New MIXERCONTROL
    4. Dim currentVol As Integer
    5. Dim type As Integer = MIXERCONTROL_CONTROLTYPE_VOLUME
    6. mixerOpen(mixer, 0, 0, 0, 0)
    7. GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, Type, volCtrl, currentVol)
    8. mixerClose(mixer)
    9. Return CInt(100 / (65535 / currentVol)) '###########################################
    10. End Function 'GetVolume


    Returnwert jetzt bis 100.

    du = CType(Marshal.PtrToStructure(pmxcd.paDetails, GetType(MIXERCONTROLDETAILS_UNSIGNED)), AudioMixerHelper.MIXERCONTROLDETAILS_UNSIGNED)
    Dies mußte ich auch Änder ,sonst lief dein Cod nicht,hat aber mit dem Wert nichts gemein.

    Gruß

    VB.NET-Quellcode

    1. Public Function GetVolume() As Integer
    2. Dim mixer As Integer
    3. Dim volCtrl As New MIXERCONTROL
    4. Dim currentVol As Integer
    5. Dim type As Integer = MIXERCONTROL_CONTROLTYPE_VOLUME
    6. mixerOpen(mixer, 0, 0, 0, 0)
    7. GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, Type, volCtrl, currentVol)
    8. mixerClose(mixer)
    9. Return CInt(100 / (65535 / currentVol)) '###########################################
    10. End Function 'GetVolume


    Returnwert jetzt bis 100.

    du = CType(Marshal.PtrToStructure(pmxcd.paDetails, GetType(MIXERCONTROLDETAILS_UNSIGNED)), AudioMixerHelper.MIXERCONTROLDETAILS_UNSIGNED)
    Dies mußte ich auch Änder ,sonst lief dein Cod nicht,hat aber mit dem Wert nichts gemein.

    Gruß

    Da hab ich jetzt echt keinen Durchblick. ?(
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils
    Hallo
    Du hast kein XP?

    Verändere mal den Regler und Starte jedesmal dein Prog neu.
    Vielleicht wird nur einmal gelesen,da du mal 100 und auch 0 gelesen hast

    Sonnst könnte es an W7 liegen ,wenn du dies benutzt, ich kann es nicht testen.

    Teste ob der Timer auch tickt!

    MfG

    Bei XP funzts

    Hab's gerade bei Windows XP auf der VM probiert. Es funktioniert.
    Wenn ich in 7 den Kompatiblitätsmodus für WinXP SP3 aktiviere funktioniert es auch. Nur kann das Debugging nicht im Kompatiblitätsmodus laufen, oder doch? Wenn ja, Wie? Oder lässt sich das auch im Code unterbringen?
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils