We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Blithe Chiang • 5 years ago

nice post!!

Spirited Warrior • 5 years ago

Thanks for the pointers! Here's my version, implemented inside a Windows Form app in conjunction with a timer that checks for sound output:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSCore.CoreAudioAPI;


namespace SoundCheckNext
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{

}


// Gets the default device for the system
public static MMDevice GetDefaultRenderDevice()
{
using (var enumerator = new MMDeviceEnumerator())
{
return enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
}
}


// Checks if audio is playing on a certain device
public static bool IsAudioPlaying(MMDevice device)
{
using (var meter = AudioMeterInformation.FromDevice(device))
{
return meter.PeakValue > 0;
}
}


private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = "Sound playing: " + IsAudioPlaying(GetDefaultRenderDevice()).ToString();
}
}
}

Ed • 7 years ago

Thanks for the code. I had to use `meter.PeakValue > 0 && meter.PeakValue < 1`, because when no sound is playing, it gives either 0, or garbage values above 1. When the sound is playing, the value is always within the range.

Abo Salah 🤙 • 7 years ago

Four years later, nice article !