控制应用程序的体积和数量VU表

时间:2015-11-19 21:47:36

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

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

我已经搜索了所有地方,似乎我只能获得当前在我的计算机上的设备的VU表并设置这些设备的音量。

即使我正在使用NAudio,我仍然愿意接受其他解决方案。

1 个答案:

答案 0 :(得分: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非常相似,因此这里的大部分代码都是可以互换的。