在c#中获取主音量

发布于 2024-08-26 21:18:09 字数 38 浏览 3 评论 0原文

我需要获取声卡输出的当前音量。

有什么想法吗?

I need to get the current volume of the output to the sound card.

Any ideas how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

西瑶 2024-09-02 21:18:09

您可以使用 IAudioMeterInformation 获取这些值Vista 和 Win 7 中的 CoreAudio API。

托管包装器可在 NAudio 中使用(从 MMDevice 获取 AudioMeterInformation)。

You can get at these values using IAudioMeterInformation in the CoreAudio APIs in Vista and Win 7.

Managed wrappers are available in NAudio (get at the AudioMeterInformation from the MMDevice).

情深缘浅 2024-09-02 21:18:09
    static int PlayerVolume()
    {
        RecordPlayer rp = new RecordPlayer();
        rp.PlayerID = -1;
        int playerVolume = rp.PlayerVolume;
        return playerVolume;
    }

来自修改后的 c# 中的麦克风音量文章

    static int PlayerVolume()
    {
        RecordPlayer rp = new RecordPlayer();
        rp.PlayerID = -1;
        int playerVolume = rp.PlayerVolume;
        return playerVolume;
    }

from modified Microphone Volume in c# article

将军与妓 2024-09-02 21:18:09

当我正在开发一个(尚未发布......)应用程序时,我解决了这个问题,该应用程序在没有其他声音存在时启动某种“电梯音乐”。

按照 Mark Heath 给出的精彩提示,我得到了我想要的:

 using NAudio.CoreAudioApi; 
 MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
 MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
 string currVolume = "MasterPeakVolume : " + defaultDevice.AudioMeterInformation.MasterPeakValue.ToString();

I solved this when I was working on a (still yet to be released...) application that initiates some kind of "elevator music" when no other sound is present.

Followed the brilliant tips given by Mark Heath, I got what I wanted :

 using NAudio.CoreAudioApi; 
 MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
 MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
 string currVolume = "MasterPeakVolume : " + defaultDevice.AudioMeterInformation.MasterPeakValue.ToString();
你丑哭了我 2024-09-02 21:18:09

在 MSDN 信息中查找:

  • IMMDeviceCollection、IMMDevice 和 IAudioEndpointVolume(仅限 Windows Vista、Windows 7)。
  • mixerGetNumDevs、mixerGetLineControls、...

这是“通用”信息。 C# 可能有更方便的方法(我不知道)。

Look in MSDN information for:

  • IMMDeviceCollection, IMMDevice and IAudioEndpointVolume (only Windows Vista, Windows 7).
  • mixerGetNumDevs, mixerGetLineControls,...

This is "common" information. It is possible C# has more convenient ways (I do not know).

单调的奢华 2024-09-02 21:18:09

也许 winmm.dll 可以帮助您:

来自 EDDYKT (VB):

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: [email protected]
    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

或者也许是这样: http ://blackbeltvb.com/index.htm?free/mcisamp.htm

Maybe the winmm.dll can help you :

From EDDYKT (VB):

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: [email protected]
    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

Or maybe this : http://blackbeltvb.com/index.htm?free/mcisamp.htm

孤独岁月 2024-09-02 21:18:09

我不相信有一种简单的方法可以在 XP 下获得当前的峰值。 MIXERC​​ONTROL_CONTROLTYPE_PEAKMETER 存在,但我相信它很大程度上不受支持(它在我当前的机器上)。我猜您将创建自己的方法来分析当前音频输出,请查看 DSP 部分 此处

您可以在运行时决定使用哪种方法,XP 和 Vista/7 有非常不同的处理音频的方法。我之前写的关于这个问题的一些可能有用的信息可以是 这里。

在我看来,MSDN 文档和 Larry Osterman(他也是 SO 的成员)博客可能是当前 Windows 音频基础结构的两个最有用的来源。

I don't believe there is an easy way to get the current Peak under XP. MIXERCONTROL_CONTROLTYPE_PEAKMETER is present but I believe it is largely unsupported (it is on my current machine). I am guessing you will have create your own method of analysing the current audio output, have a look at the DSP section here.

You can just decide at runtime which method you would like to use, XP and Vista/7 have very different methods of dealing with the audio. Some possibly useful information on this matter I wrote previously can be here.

The MSDN documentation and Larry Osterman's (he is also a member on SO) blog are probably the 2 most useful sources for current windows audio infrastructure in my opinion.

时光病人 2024-09-02 21:18:09

查看代码项目中的此代码:使用 DirectX 的 LED 风格音量表

本文作为使用手册
对于我创建的名为的 UserControl
模拟信号表。该控件使用
Direct3D 绘制控件,以及
DirectSound 对音频进行采样
信号。

它有一个 AnalogSignalMeter 对象,该对象会触发一个事件,报告当前左右扬声器的电平。

Check out this code from Code Project: LED Style Volume Meter Using DirectX

This article serves as a usage manual
for a UserControl I created called
AnalogSignalMeter. This control uses
Direct3D to paint the control, and
DirectSound to sample the audio
signal.

It has an AnalogSignalMeter object, that is fired an event which will report the current left and right speaker level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文