【问题标题】:How to access current system audio sound to see actual sound volume?如何访问当前系统音频声音以查看实际音量?
【发布时间】:2012-06-07 15:49:31
【问题描述】:

我需要获得在 Windows 7 系统上播放的实际级别的音频,就像 Skype 在设置中那样:

我没有找到任何关于它的信息,这里有人可以帮助我吗?

我想做的是,我想写一个简单的工具,如果窗户上的声音太小,它会调高最大音量,如果声音太大,它会调低音量。

【问题讨论】:

  • 我明白你的意思。可能有一种方法可以使用 NAudio (naudio.codeplex.com) 从输出设备开始录制,从而以这种方式获得电平。今晚晚些时候我会检查一下,如果我弄明白了,我会发帖回答。

标签: c# audio volume


【解决方案1】:

你可以试试这个链接:

http://www.dreamincode.net/forums/topic/45693-controlling-sound-volume-in-c%23/

基本上,你需要导入这个win32 api函数:

[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

那么你可以这样使用它:

waveOutGetVolume(IntPtr.Zero, out CurrVol);

你会得到'CurrVol'的音量

【讨论】:

  • 问题不是询问音量级别(在音量混合器中设置)。问题是询问输入通道上的信号电平(后混频器)。这将需要对信号进行采样并检测绝对信号峰值(可能在短时间内)
【解决方案2】:

下载 NAudio 类并在 C# 项目中引用 DLL。

然后将以下代码添加到您的项目中。 它将枚举所有音频设备并获取其音量级别并尝试将其静音。

        try
        {
            //Instantiate an Enumerator to find audio devices
            NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator();
            //Get all the devices, no matter what condition or status
            NAudio.CoreAudioApi.MMDeviceCollection DevCol = MMDE.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.All);
            //Loop through all devices
            foreach (NAudio.CoreAudioApi.MMDevice dev in DevCol)
            {
                try
                {
                    //Get its audio volume
                    System.Diagnostics.Debug.Print("Volume of " + dev.FriendlyName + " is " + dev.AudioEndpointVolume.MasterVolumeLevel.ToString());

                    //Mute it
                    dev.AudioEndpointVolume.Mute = true;
                    System.Diagnostics.Debug.Print(dev.FriendlyName + " is muted");

                    //Get its audio volume
                    System.Diagnostics.Debug.Print(dev.AudioEndpointVolume.MasterVolumeLevel.ToString());
                }
                catch (Exception ex)
                {
                    //Do something with exception when an audio endpoint could not be muted
                    System.Diagnostics.Debug.Print(dev.FriendlyName + " could not be muted");
                }
            }
        }
        catch (Exception ex)
        {
            //When something happend that prevent us to iterate through the devices
            System.Diagnostics.Debug.Print("Could not enumerate devices due to an excepion: " + ex.Message);
        }

【讨论】:

  • 嗨,迈克,我复制了您的代码,但遇到了问题。我无法获得MMDeviceCollection,因为MMDeviceEnumerator 似乎没有初始化。每当我尝试获取MMDeviceCollection 时,总是会出现错误Object reference not set to an instance of an object。我在这里错过了什么吗?
  • @AnnaFortuna 你用新的构造函数实例化了NAudio.CoreAudioApi.MMDeviceEnumerator MMDE吗?
  • 我是这样实例化的:NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator();
猜你喜欢
  • 2011-07-25
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多