using System; using System.Collections.Generic; namespace RobvanderWoude { internal class DetectSpeakers { static readonly string progver = "1.00"; static int Main( string[] args ) { if ( args.Length > 0 ) { return ShowHelp( ); } try { int count = NAudio.Wave.WaveOut.DeviceCount; Console.WriteLine( "{0} Audio output device{1} detected", count, ( count == 1 ? "" : "s" ) ); NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator( ); NAudio.CoreAudioApi.MMDeviceCollection devices = MMDE.EnumerateAudioEndPoints( NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.Active ); if ( devices.Count == 1 ) { Console.WriteLine( "Active device: {0}", devices[0].DeviceFriendlyName ); Console.WriteLine( "Master volume: {0}", devices[0].AudioEndpointVolume.MasterVolumeLevel ); } else if ( devices.Count > 1 ) { Console.WriteLine( "Active devices' master volume:" ); foreach ( NAudio.CoreAudioApi.MMDevice device in devices ) { Console.WriteLine( "{0}: {1}", device.DeviceFriendlyName, device.AudioEndpointVolume.MasterVolumeLevel ); } } return count; } catch ( Exception e ) { return ShowHelp( e.Message ); } } #region Error handling static int ShowHelp( params string[] errmsg ) { #region Error Message if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } #endregion Error Message #region Help Text /* DetectSpeakers.exe, Version 1.00 Check if speakers or headphones are connected Usage: DetectSpeakers.exe Notes: Return code ("errorlevel") equals the number of active audio output devices detected, or -1 in case of errors. This program uses NAudio by Mark Heath et al. NAudio files required (in this program's parent folder) are NAudio.Core.dll, NAudio.Wasapi.dll, NAudio.WinForms.dll and NAudio.WinMM.dll, all included in this program's ZIP file. Consider this program experimental: it had only limited testing, due mainly to my computer's limited audio hardware. Credits: NAudio library for .NET by Mark Heath et al https://github.com/naudio/NAudio Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Display Help Text Console.Error.WriteLine( ); Console.Error.WriteLine( "DetectSpeakers.exe, Version {0}", progver ); Console.Error.WriteLine( "Check if speakers or headphones are connected" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "DetectSpeakers.exe" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Notes: Return code (\"errorlevel\") equals the number of active" ); Console.Error.WriteLine( " audio output devices detected, or -1 in case of errors." ); Console.Error.WriteLine( " This program uses NAudio by Mark Heath et al." ); Console.Error.WriteLine( " NAudio files required (in this program's parent folder) are" ); Console.Error.WriteLine( " NAudio.Core.dll, NAudio.Wasapi.dll, NAudio.WinForms.dll and" ); Console.Error.WriteLine( " NAudio.WinMM.dll, all included in this program's ZIP file." ); Console.Error.WriteLine( " Consider this program experimental: it had only limited" ); Console.Error.WriteLine( " testing, due mainly to my computer's limited audio hardware." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Credits: NAudio library for .NET by Mark Heath et al" ); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Error.WriteLine( " https://github.com/naudio/NAudio" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Display Help Text return -1; } #endregion Error handling } }