【问题标题】:Controlling Application's Volume & VU Meter控制应用程序的音量和 VU 表
【发布时间】:2023-03-14 11:33:01
【问题描述】:

我正在将 NAudio 用于我正在设计的屏幕录制软件,我需要知道是否可以不仅控制特定应用程序的音量,还可以显示应用程序声音的 VU Meter。

我用谷歌搜索了所有地方,似乎我只能为我计算机上当前的设备获取 VU Meter 并为这些设备设置音量。

即使我使用的是 NAudio,我也愿意接受其他解决方案。

【问题讨论】:

    标签: c# wpf visual-studio visual-studio-2013 naudio


    【解决方案1】:

    我在这个问题之后更详细地问了这个问题。我已经找到了答案,所以我会把答案留给那些偶然发现它的人。尝试使用 NAudio 和 CSCore 让我非常熟悉,所以请询问您是否需要进一步的帮助。

    此代码块使用 CSCore,是此处找到的答案的修改和注释版本:Getting individual windows application current volume output level as visualized in audio Mixer

    class PeakClass
    {
        static int CurrentProcessID = 0000;
    
        private static void Main(string[] args)
        {
            //Basically gets your default audio device and session attached to it
            using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
            {
                using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
                {
                    //This will go through a list of all processes uses the device
                    //the code got two line above.
                    foreach (var session in sessionEnumerator)
                    {
                        //This block of code will get the peak value(value needed for VU Meter)
                        //For whatever process you need it for (I believe you can also check by name
                        //but I found that less reliable)
                        using (var session2 = session.QueryInterface<AudioSessionControl2>())
                        {
                            if(session2.ProcessID == CurrentProcessID)
                            {
                                using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
                                {
                                    Console.WriteLine(audioMeterInformation.GetPeakValue());
                                }
                            }
                        }
    
                       //Uncomment this block of code if you need the peak values 
                       //of all the processes
                       //
                        //using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
                        //{
                        //    Console.WriteLine(audioMeterInformation.GetPeakValue());
                        //}
                    }
                }
            }
        }
    
        private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
        {
            using (var enumerator = new MMDeviceEnumerator())
            {
                using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
                {
                    Console.WriteLine("DefaultDevice: " + device.FriendlyName);
                    var sessionManager = AudioSessionManager2.FromMMDevice(device);
                    return sessionManager;
                }
            }
        }
    } 
    

    以下代码块将允许您使用 NAudio 更改设备的音量

    MMDevice VUDevice;
    
    public void SetVolume(float vol)
        {
            if(vol > 0)
            {
                VUDevice.AudioEndpointVolume.Mute = false;
                VUDevice.AudioEndpointVolume.MasterVolumeLevelScalar = vol;
            }
            else
            {
                VUDevice.AudioEndpointVolume.Mute = true;
            }
            Console.WriteLine(vol);
        }
    

    我有来自两个不同库的代码,只是为了回答我直接发布的问题,即如何设置音量和获取 VU Meter 值(峰值)。 CSCore 和 NAudio 非常相似,所以这里的大部分代码都是可以互换的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      • 2011-12-18
      • 2011-06-04
      • 2014-01-20
      • 1970-01-01
      相关资源
      最近更新 更多