8

I am trying to write a C# code that outputs the current audio output level from each of the windows application accessing the sound output (as shown with constantly changing green bars of the Volume mixer).

The program will check every 10 ms, and outputs sth like this: Windows Media Player: 30, Mozilla Firefox: 0, Adobe Flash Player: 35 (as per the figure)

I am using Windows 7, and trying it in C# (as Java cannot achieve this).

I have found ways to get and set the Master Volume (the handle bar which shows 65% for Windows Media Player) for a running application, is there a way to get the green fluctuating level data?

Thank you!

Audio Mixer

Florian
  • 5,728
  • 3
  • 41
  • 75
mio
  • 159
  • 1
  • 1
  • 11

2 Answers2

18

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.

Axiumin_
  • 1,751
  • 2
  • 14
  • 20
Florian
  • 5,728
  • 3
  • 41
  • 75
  • Thanks thefiloe for the response. As mentioned in the question, getting/setting master volume (slider max value in the mixer image) has been answered before: http://stackoverflow.com/questions/14306048/controling-volume-mixer. I am trying to read the green value shown on the mixer image, i.e. current audio output volume, which keeps on changing and is always less than equal to the max Master Volume. – mio Jan 18 '14 at 18:10
  • I'm sorry. Had no time to read your whole question. I've edited my answer. – Florian Jan 18 '14 at 18:47
  • Thank you verry much, I also used your previous answer :) because this is a way simpeler approch than the linked answer. Thank you verry much – Koen Van Looveren Nov 28 '16 at 15:22
1

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

enter image description here

David Jeske
  • 1,866
  • 16
  • 24