Windows Master Volume stellen

  • VB.NET

Es gibt 13 Antworten in diesem Thema. Der letzte Beitrag () ist von thefiloe.

    Windows Master Volume stellen

    Hi

    Ich nehme mal an, das folgende Frage schon hunderte mal gestellt wurde, aber: Wie stelle ich die Hauptlautstärke und Balance für Windows ein (mithilfe von netframework 4).

    Alle Ansätze im Netz haben mich irgendwie immer gegen ne Wand gesetzt, weil irgendwelche Objekte nicht existierten. So schwer kann es doch eigentlich gar nicht sein.

    Hoffe, ihr könnt mir da helfen.
    Liebe Grüße
    sothere
    Kannste mit meiner Lib machen. Link ist im Anhang. Installation am Besten über NuGet. Brauchste die AudioEndpointVolume Klasse aus dem CSCore.CoreAudioAPI namespace.
    Mehr infos findest du hier: [VB.NET] Systemlautstärke auslesen und ändern


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.
    Glaub mir, es geht nur über WASAPI, alle anderen APIs setzen nur entweder die Lautstärke des eigenen Programmes oder die Lautstärke der Wiedergabe. Das liegt einfach daran, dass seit Windows Vista alles nur noch über WASAPI läuft und auch die Geräte von WASAPI verwaltet werden. Alles andere wurde dann darauf angepasst, aber die alten APIs hatten da halt noch keine Unterscheidung und sie wurden auch nicht entsprechend erweitert, da Microsoft eben nur noch WASAPI entwickelt.
    Wenn du den Ton schon anderweitig abspielst ist das doch egal. Du kannst damit trotzdem die Systemlautstärke ändern.
    Mit WASAPI oder genauer CoreAudioAPI bist du sicher am Besten beraten(läuft aber nur Vista+). Du kannst damit sehr schnell und einfach die Lautstärke setzen. Falls du Hilfe beim Code brauchst, kann ich dir helfen(die Lib ist leider nicht wirklich dokumentiert). Ach ja falls es unter auch auf XP,... laufen soll, solltest du im Fall von XP die CSCore.SoundOut.MMInterop.SetVolume bzw. CSCore.SoundOut.MMInterop.GetVolume Methode verwenden. MMInterop.SetVolume(IntPtr.Zero, 1, 1) entspricht 100% und 0 anstatt der 1 ist dann 0%. 0.5 = 50% @sothere


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „thefiloe“ ()

    Du könntest ja auch einfach mal den Code von thefiloe anschauen und versuchen, ihn zu verstehen. Das ist ja eine der schönen Nebeneffekte bei OpenSource, man kann ziemlich einfach ziemlich viel dabei lernen :).
    Hallo,

    mit der winmm.dll funktioniert es auch, hab irgendwann mal ein VB6 Sample gefunden und .Net-conform umgeschrieben.
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class Mixer
    3. Const MMSYSERR_NOERROR As Integer = 0
    4. Const MAXPNAMELEN As Integer = 32
    5. Const MIXER_LONG_NAME_CHARS As Integer = 64
    6. Const MIXER_SHORT_NAME_CHARS As Integer = 16
    7. Const MIXER_GETLINEINFOF_COMPONENTTYPE As Integer = &H3
    8. Const MIXER_GETCONTROLDETAILSF_VALUE As Integer = &H0
    9. Const MIXER_GETLINECONTROLSF_ONEBYTYPE As Integer = &H2
    10. Const MIXER_SETCONTROLDETAILSF_VALUE As Integer = &H0
    11. Const MIXERLINE_COMPONENTTYPE_DST_FIRST As Integer = &H0
    12. Const MIXERLINE_COMPONENTTYPE_SRC_FIRST As Integer = &H1000
    13. Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS As Integer = MIXERLINE_COMPONENTTYPE_DST_FIRST + 4
    14. Const MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE As Integer = MIXERLINE_COMPONENTTYPE_SRC_FIRST + 3
    15. Const MIXERLINE_COMPONENTTYPE_SRC_LINE As Integer = MIXERLINE_COMPONENTTYPE_SRC_FIRST + 2
    16. Const MIXERCONTROL_CT_CLASS_FADER As Integer = &H50000000
    17. Const MIXERCONTROL_CT_UNITS_UNSIGNED As Integer = &H30000
    18. Const MIXERCONTROL_CONTROLTYPE_FADER As Integer = MIXERCONTROL_CT_CLASS_FADER Or MIXERCONTROL_CT_UNITS_UNSIGNED
    19. Const MIXERCONTROL_CONTROLTYPE_VOLUME As Integer = MIXERCONTROL_CONTROLTYPE_FADER + 1
    20. <DllImport("winmm.dll")> _
    21. Public Shared Function mixerClose(ByVal hmx As IntPtr) As Integer
    22. End Function
    23. <DllImport("winmm.dll")> _
    24. Public Shared Function mixerGetControlDetails(<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
    25. End Function
    26. <DllImport("winmm.dll")> _
    27. Public Shared Function mixerGetLineControls(ByVal hmxobj As IntPtr, ByRef pmxlc As MIXERLINECONTROLS, ByVal fdwControls As Integer) As Integer
    28. End Function
    29. <DllImport("winmm.dll")> _
    30. Shared Function mixerGetLineInfo(<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As MixerFlags) As MMRESULT
    31. End Function
    32. <DllImport("winmm.dll")> _
    33. Public Shared Function mixerOpen(ByRef phmx As IntPtr, ByVal uMxId As UInteger, ByVal dwCallback As IntPtr, ByVal dwInstance As IntPtr, ByVal fdwOpen As UInteger) As Integer
    34. End Function
    35. <DllImport("winmm.dll")> _
    36. Private Shared Function mixerSetControlDetails(ByVal hmxobj As IntPtr, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
    37. End Function
    38. <Flags()> _
    39. Public Enum MixerFlags
    40. MIXER_GETLINEINFOF_DESTINATION = 0
    41. MIXER_GETLINEINFOF_SOURCE = 1
    42. MIXER_GETLINEINFOF_LINEID = 2
    43. MIXER_GETLINEINFOF_COMPONENTTYPE = 3
    44. MIXER_GETLINEINFOF_TARGETTYPE = 4
    45. MIXER_OBJECTF_MIXER = &H0
    46. MIXER_OBJECTF_HMIXER = &H80000000
    47. MIXER_OBJECTF_WAVEOUT = &H10000000
    48. MIXER_OBJECTF_HWAVEOUT = &H90000000
    49. MIXER_OBJECTF_WAVEIN = &H20000000
    50. MIXER_OBJECTF_HWAVEIN = &HA0000000
    51. MIXER_OBJECTF_MIDIOUT = &H30000000
    52. MIXER_OBJECTF_HMIDIOUT = &HB0000000
    53. MIXER_OBJECTF_MIDIIN = &H40000000
    54. MIXER_OBJECTF_HMIDIIN = &HC0000000
    55. MIXER_OBJECTF_AUX = &H50000000
    56. End Enum
    57. Enum MMRESULT
    58. MMSYSERR_NOERROR = 0
    59. MMSYSERR_ERROR = 1
    60. MMSYSERR_BADDEVICEID = 2
    61. MMSYSERR_NOTENABLED = 3
    62. MMSYSERR_ALLOCATED = 4
    63. MMSYSERR_INVALHANDLE = 5
    64. MMSYSERR_NODRIVER = 6
    65. MMSYSERR_NOMEM = 7
    66. MMSYSERR_NOTSUPPORTED = 8
    67. MMSYSERR_BADERRNUM = 9
    68. MMSYSERR_INVALFLAG = 10
    69. MMSYSERR_INVALPARAM = 11
    70. MMSYSERR_HANDLEBUSY = 12
    71. MMSYSERR_INVALIDALIAS = 13
    72. MMSYSERR_BADDB = 14
    73. MMSYSERR_KEYNOTFOUND = 15
    74. MMSYSERR_READERROR = 16
    75. MMSYSERR_WRITEERROR = 17
    76. MMSYSERR_DELETEERROR = 18
    77. MMSYSERR_VALNOTFOUND = 19
    78. MMSYSERR_NODRIVERCB = 20
    79. WAVERR_BADFORMAT = 32
    80. WAVERR_STILLPLAYING = 33
    81. WAVERR_UNPREPARED = 34
    82. End Enum
    83. Public Structure MIXERCAPS
    84. Public wMid As Integer
    85. Public wPid As Integer
    86. Public vDriverVersion As Integer
    87. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String
    88. Public fdwSupport As Integer
    89. Public cDestinations As Integer
    90. End Structure
    91. Public Structure MIXERCONTROL
    92. Public cbStruct As Integer
    93. Public dwControlID As Integer
    94. Public dwControlType As Integer
    95. Public fdwControl As Integer
    96. Public cMultipleItems As Integer
    97. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String
    98. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String
    99. Public lMinimum As Integer
    100. Public lMaximum As Integer
    101. <MarshalAs(UnmanagedType.U4, SizeConst:=10)> Public reserved As Integer
    102. End Structure
    103. Public Structure MIXERCONTROLDETAILS
    104. Public cbStruct As Integer
    105. Public dwControlID As Integer
    106. Public cChannels As Integer
    107. Public item As Integer
    108. Public cbDetails As Integer
    109. Public paDetails As IntPtr
    110. End Structure
    111. Public Structure MIXERCONTROLDETAILS_UNSIGNED
    112. Public dwValue As Integer
    113. End Structure
    114. Public Structure MIXERLINE
    115. Public cbStruct As Integer
    116. Public dwDestination As Integer
    117. Public dwSource As Integer
    118. Public dwLineID As Integer
    119. Public fdwLine As Integer
    120. Public dwUser As Integer
    121. Public dwComponentType As Integer
    122. Public cChannels As Integer
    123. Public cConnections As Integer
    124. Public cControls As Integer
    125. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String
    126. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String
    127. Public dwType As Integer
    128. Public dwDeviceID As Integer
    129. Public wMid As Integer
    130. Public wPid As Integer
    131. Public vDriverVersion As Integer
    132. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String
    133. End Structure
    134. Public Structure MIXERLINECONTROLS
    135. Public cbStruct As Integer
    136. Public dwLineID As Integer
    137. Public dwControl As Integer
    138. Public cControls As Integer
    139. Public cbmxctrl As Integer
    140. Public pamxctrl As IntPtr
    141. End Structure
    142. Public Function GetVolumeControl(ByVal hmixer As Integer, ByVal componentType As Integer, ByVal ctrlType As Integer, ByRef mxc As MIXERCONTROL, ByRef vCurrentVol As Integer) As Boolean
    143. Dim mxlc As New MIXERLINECONTROLS
    144. Dim mxl As New MIXERLINE
    145. Dim pmxcd As New MIXERCONTROLDETAILS
    146. Dim du As New MIXERCONTROLDETAILS_UNSIGNED
    147. mxc = New MIXERCONTROL
    148. Dim rc As Integer
    149. Dim retValue As Boolean
    150. vCurrentVol = -1
    151. mxl.cbStruct = Marshal.SizeOf(mxl)
    152. mxl.dwComponentType = componentType
    153. rc = mixerGetLineInfo(hmixer, mxl, CType(MIXER_GETLINEINFOF_COMPONENTTYPE, MixerFlags))
    154. If MMSYSERR_NOERROR = rc Then
    155. Dim sizeofMIXERCONTROL As Integer = 152
    156. Dim ctrl As Integer = Marshal.SizeOf(GetType(MIXERCONTROL))
    157. mxlc.pamxctrl = Marshal.AllocCoTaskMem(sizeofMIXERCONTROL)
    158. mxlc.cbStruct = Marshal.SizeOf(mxlc)
    159. mxlc.dwLineID = mxl.dwLineID
    160. mxlc.dwControl = ctrlType
    161. mxlc.cControls = 1
    162. mxlc.cbmxctrl = sizeofMIXERCONTROL
    163. mxc.cbStruct = sizeofMIXERCONTROL
    164. rc = mixerGetLineControls(CType(hmixer, IntPtr), mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE)
    165. If MMSYSERR_NOERROR = rc Then
    166. retValue = True
    167. mxc = CType(Marshal.PtrToStructure(mxlc.pamxctrl, GetType(MIXERCONTROL)), MIXERCONTROL)
    168. Else : retValue = False
    169. End If
    170. Dim sizeofMIXERCONTROLDETAILS As Integer = Marshal.SizeOf(GetType(MIXERCONTROLDETAILS))
    171. Dim sizeofMIXERCONTROLDETAILS_UNSIGNED As Integer = Marshal.SizeOf(GetType(MIXERCONTROLDETAILS_UNSIGNED))
    172. pmxcd.cbStruct = sizeofMIXERCONTROLDETAILS
    173. pmxcd.dwControlID = mxc.dwControlID
    174. pmxcd.paDetails = Marshal.AllocCoTaskMem(sizeofMIXERCONTROLDETAILS_UNSIGNED)
    175. pmxcd.cChannels = 1
    176. pmxcd.item = 0
    177. pmxcd.cbDetails = sizeofMIXERCONTROLDETAILS_UNSIGNED
    178. rc = mixerGetControlDetails(hmixer, pmxcd, MIXER_GETCONTROLDETAILSF_VALUE)
    179. du = CType(Marshal.PtrToStructure(pmxcd.paDetails, GetType(MIXERCONTROLDETAILS_UNSIGNED)), MIXERCONTROLDETAILS_UNSIGNED)
    180. vCurrentVol = du.dwValue
    181. Return retValue
    182. End If
    183. retValue = False
    184. Return retValue
    185. End Function
    186. Private Function SetVolumeControl(ByVal hmixer As Integer, ByVal mxc As MIXERCONTROL, ByVal volume As Integer) As Boolean
    187. Dim retValue As Boolean
    188. Dim rc As Integer
    189. Dim mxcd As New MIXERCONTROLDETAILS
    190. Dim vol As New MIXERCONTROLDETAILS_UNSIGNED
    191. mxcd.item = 0
    192. mxcd.dwControlID = mxc.dwControlID
    193. mxcd.cbStruct = Marshal.SizeOf(mxcd)
    194. mxcd.cbDetails = Marshal.SizeOf(vol)
    195. mxcd.cChannels = 1
    196. vol.dwValue = volume
    197. mxcd.paDetails = Marshal.AllocCoTaskMem(Marshal.SizeOf(GetType(MIXERCONTROLDETAILS_UNSIGNED)))
    198. Marshal.StructureToPtr(vol, mxcd.paDetails, False)
    199. rc = mixerSetControlDetails(CType(hmixer, IntPtr), mxcd, MIXER_SETCONTROLDETAILSF_VALUE)
    200. If MMSYSERR_NOERROR = rc Then
    201. retValue = True
    202. Else : retValue = False
    203. End If
    204. Return retValue
    205. End Function
    206. Public Function GetVolume() As Integer
    207. Dim volCtrl As New MIXERCONTROL
    208. Dim currentVol As Integer
    209. mixerOpen(IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero, 0)
    210. GetVolumeControl(0, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, MIXERCONTROL_CONTROLTYPE_VOLUME, volCtrl, currentVol)
    211. mixerClose(IntPtr.Zero)
    212. Return CInt(10 / (65535 / currentVol))
    213. End Function
    214. Public Sub SetVolume(ByVal vVolume As Integer)
    215. Dim volCtrl As New MIXERCONTROL
    216. Dim currentVol As Integer
    217. mixerOpen(IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero, 0)
    218. GetVolumeControl(0, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, MIXERCONTROL_CONTROLTYPE_VOLUME, volCtrl, currentVol)
    219. If vVolume > volCtrl.lMaximum Then
    220. vVolume = volCtrl.lMaximum
    221. End If
    222. If vVolume < volCtrl.lMinimum Then
    223. vVolume = volCtrl.lMinimum
    224. End If
    225. Dim L As Integer = CInt(65535 * vVolume / 10)
    226. SetVolumeControl(0, volCtrl, L)
    227. GetVolumeControl(0, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, MIXERCONTROL_CONTROLTYPE_VOLUME, volCtrl, currentVol)
    228. If L <> currentVol Then
    229. Throw New Exception("Can´t Set Volume")
    230. End If
    231. mixerClose(IntPtr.Zero)
    232. End Sub
    233. End Class


    mfG

    Derfuhr

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