Reputation: 15926
How can I figure out if Windows is currently playing any sounds through the primary audio device? I need to know, so that I can make my program automatically adjust its volume.
Upvotes: 2
Views: 2728
Reputation: 5368
You could use CSCore.
Download it right here -> http://cscore.codeplex.com/
Paste these lines on a console project.
using System;
using CSCore.CoreAudioAPI;
namespace AudioDetector
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(IsAudioPlaying(GetDefaultRenderDevice()));
Console.ReadLine();
}
public static MMDevice GetDefaultRenderDevice()
{
using (var enumerator = new MMDeviceEnumerator())
{
return enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
}
}
public static bool IsAudioPlaying(MMDevice device)
{
using (var meter = AudioMeterInformation.FromDevice(device))
{
return meter.PeakValue > 0;
}
}
}
}
Play a music be it on YouTube, Music Player, etc...
Run the program.
It automatically notifies(true/false) if there is an audio currently being played or not.
Upvotes: 6
Reputation: 5408
You may need to mess around with mixer controls.
These may help ya out too.
Measure speaker volume by recording playing sound with microphone
Using p/invoke and win-api to monitor audio line-in (C#)
Upvotes: 2