控制应用程序的音量和 VU 表

Controlling Application's Volume & VU Meter

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

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

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

这个问题之后我问的更详细了。我已经找到了答案,所以我会把答案留给那些偶然发现它的人。尝试使用 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 表值(峰值)。 CSCore 和 NAudio 非常相似,因此这里的大部分代码都可以互换。