Getting individual windows application current volume output level as visualized in audio Mixer

元气小坏坏 提交于 2019-11-26 22:30:59

You can use CSCore. There is a wrapper for the CoreAudioAPI-Audiosessions. Use something like that (for more details take a look at the unittests: AudioSession-UnitTests):

private static void Main(string[] args)
{
    using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
    {
        using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
        {
            foreach (var session in sessionEnumerator)
            {
                using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
                {
                    Console.WriteLine(audioMeterInformation.GetPeakValue());
                }
            }
        }
    }

    Console.ReadKey();
}

private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
{
    using (var enumerator = new MMDeviceEnumerator())
    {
        using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
        {
            Debug.WriteLine("DefaultDevice: " + device.FriendlyName);
            var sessionManager = AudioSessionManager2.FromMMDevice(device);
            return sessionManager;
        }
    }
}

To control an applications volume, take a look at the unit-tests here: http://cscore.codeplex.com/SourceControl/latest#CSCore.Test/CoreAudioAPI/AudioSessionTests.cs

Here is a sample application which displays the audio levels from running applications in a graph. There are two versions, one in WPF and one in Windows.Forms. They use the method from Florian's answer to get the audio levels.

https://github.com/jeske/SoundLevelMonitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!