Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace WindowsMicrophoneMuteLibrary
  6. {
  7.     /// <summary>
  8.     /// Built by Matt Palmerlee November 2010
  9.     /// For muting and unmuting the microphone using C# on Windows XP, Vista, and Windows 7
  10.     /// Uses parts of Gustavo Franco's MixerNative AudioLib source for Windows XP and older from here:
  11.     /// http://www.codeguru.com/csharp/csharp/cs_graphics/sound/article.php/c10931
  12.     /// And uses Ray Molenkamp's C# managed wrapper for accessing the Vista Core Audio API (for Windows Vista and newer)
  13.     /// http://www.codeproject.com/KB/vista/CoreAudio.aspx?msg=2489276
  14.     /// Other references:
  15.     /// http://stackoverflow.com/questions/2078970/how-to-mute-the-microphone-c
  16.     /// http://stackoverflow.com/questions/154089/mute-windows-volume-using-c
  17.     /// http://stackoverflow.com/questions/3046668/how-to-mute-microphone-in-windows-7-with-c-c
  18.     /// </summary>
  19.     public class WindowsMicMute
  20.     {
  21.         private CoreAudioMicMute vistaMicMute = null;
  22.  
  23.         public WindowsMicMute()
  24.         {
  25.             try
  26.             {
  27.                 this.vistaMicMute = new CoreAudioMicMute();
  28.                 //for some reason I had to call this to setup the Audio Interfaces on startup instead of later or I get invalid cast com exceptions (I think because it was all initialized from separate threads)
  29.                 //this.vistaMicMute.SetMute(true);
  30.                 //this.vistaMicMute.SetMute(false);
  31.             }
  32.             catch
  33.             {
  34.                 this.vistaMicMute = null;//we'll try the old (xp) way
  35.             }
  36.         }
  37.  
  38.         public void MuteMic()
  39.         {
  40.             if (this.vistaMicMute != null)
  41.             {
  42.                 this.vistaMicMute.SetMute(true);
  43.             }
  44.             else
  45.                 MixerNativeLibrary.MicInterface.MuteOrUnMuteAllMics(true);
  46.         }
  47.         public bool getMuteStatus()
  48.         {
  49.             return this.vistaMicMute.getMute();
  50.            
  51.         }
  52.  
  53.         public void UnMuteMic()
  54.         {
  55.             if (this.vistaMicMute != null)
  56.             {
  57.                 this.vistaMicMute.SetMute(false);
  58.             }
  59.             else
  60.                 MixerNativeLibrary.MicInterface.MuteOrUnMuteAllMics(false);
  61.         }
  62.  
  63.  
  64.     }
  65. }