예제 #1
0
 /// <summary>
 /// The Constructor
 /// </summary>
 public AudioDeviceController()
 {
     _deviceEnumerator   = MMDeviceEnumeratorFactory.CreateInstance();
     _deviceNotification = new MMNotificationClient();
     _deviceEnumerator.RegisterEndpointNotificationCallback(_deviceNotification);
     _deviceNotification.RegisterAudioDeviceStateChange(OnAudioDeviceChange);
 }
예제 #2
0
        /// <summary>
        /// Tries to set the volume to a specified level
        /// </summary>
        /// <returns>returns true if the keypress was sent, false if there was an error</returns>
        public bool SetVolume(int Volume)
        {
            try
            {
                IMMDeviceEnumerator enumerator = MMDeviceEnumeratorFactory.CreateInstance();
                IMMDevice           device;

                int eRender     = 0;
                int eMultimedia = 1;

                enumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out device);

                object endpoint = null;
                device.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out endpoint);

                IAudioEndpointVolume audio = (IAudioEndpointVolume)endpoint;

                if (audio.SetMasterVolumeLevelScalar(Volume / 100f, new Guid()) != 0)
                {
                    throw new Exception();
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
예제 #3
0
        private static ISimpleAudioVolume GetVolumeObject(int?pid)
        {
            IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();

            deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice speakers);

            Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;

            speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out object o);
            IAudioSessionManager2 mgr = (IAudioSessionManager2)o;

            mgr.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator);
            sessionEnumerator.GetCount(out int count);

            ISimpleAudioVolume volumeControl = null;

            for (int i = 0; i < count; i++)
            {
                sessionEnumerator.GetSession(i, out IAudioSessionControl2 ctl);
                ctl.GetProcessId(out int cpid);

                if (cpid == pid)
                {
                    volumeControl = ctl as ISimpleAudioVolume;
                    break;
                }
                Marshal.ReleaseComObject(ctl);
            }
            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);
            return(volumeControl);
        }
예제 #4
0
        /// <summary>
        /// Tries to get the currently set volume
        /// </summary>
        /// <returns>either the current audio volume or null if the level could not be read</returns>
        public Nullable <int> GetCurrentVolume()
        {
            try
            {
                float level = 0;

                IMMDeviceEnumerator enumerator = MMDeviceEnumeratorFactory.CreateInstance();
                IMMDevice           device;

                int eRender     = 0;
                int eMultimedia = 1;

                enumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out device);

                object endpoint = null;
                device.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out endpoint);

                IAudioEndpointVolume audio = (IAudioEndpointVolume)endpoint;

                audio.GetMasterVolumeLevelScalar(ref level);

                return((int)(level * 100));
            }
            catch
            {
                return(null);
            }
        }
예제 #5
0
        public static bool?IsMicrophoneMute()
        {
            bool mute = false;

            try
            {
                IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
                IMMDevice           microphone       = null;
                deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications, out microphone);
                if (microphone != null)
                {
                    object aepv_obj;
                    microphone.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
                    IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
                    int v   = 0;
                    int res = aepv.GetMute(out v);
                    mute = (v == 1);

                    Console.WriteLine($"Audio capture mute state is {mute}");
                    return(mute);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"**Could not read Audio capture mute state ** {ex.Message}");
            }
            return(null);
        }
예제 #6
0
 public static int SetMicrophoneMasterVolume(float volume)
 {
     if (volume < 0 || volume > 1)
     {
         throw new ArgumentOutOfRangeException("Provide volumet between 0 and 1");
     }
     try
     {
         IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
         IMMDevice           microphone       = null;
         deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications, out microphone);
         if (microphone != null)
         {
             object aepv_obj;
             microphone.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
             IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
             Guid ZeroGuid             = new Guid();
             int  res = aepv.SetMasterVolumeLevelScalar(volume, ZeroGuid);
             Console.WriteLine($"Audio microphone level set to {volume}%");
             return(res);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine($"**Could not set microphone level** {ex.Message}");
         return(ex.HResult);
     }
     return(1); // no mic
 }
예제 #7
0
        public static int SetMicrophoneMute(bool mute)
        {
            try
            {
                IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
                IMMDevice           microphone       = null;
                deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications, out microphone);
                if (microphone != null)
                {
                    object aepv_obj;
                    microphone.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
                    IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
                    Guid ZeroGuid             = new Guid();
                    int  v   = mute ? 1 : 0;
                    int  res = aepv.SetMute(v, ref ZeroGuid);

                    Console.WriteLine($"Audio mute is set to {mute}");
                    return(0);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"**Could not set Audio capture mute state** {ex.Message}");
                return(ex.HResult);
            }
            return(1); // no mic
        }
예제 #8
0
        public AudioControl(AudioDataFlow audioFlow)
        {
            _audioDataFlow = audioFlow;
            //Create Instance
            IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();

            InitializeAudio(audioFlow, deviceEnumerator);
        }
예제 #9
0
        public static void setVolume(int level)
        {
            IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
            IMMDevice           speakers;
            const int           eRender     = 0;
            const int           eMultimedia = 1;

            deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers);
            object aepv_obj;

            speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
            IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
            Guid ZeroGuid             = new Guid();

            aepv.SetMasterVolumeLevelScalar(level / 100f, ZeroGuid);
        }
예제 #10
0
        public static void SetVol(int Level)
        {
            try
            {
                IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
                IMMDevice           speakers;
                const int           eRender     = 0;
                const int           eMultimedia = 1;
                deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers);

                object aepv_obj;
                speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
                IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
                Guid     ZeroGuid         = new Guid();
                int      res           = aepv.SetMasterVolumeLevelScalar(Level / 100f, ZeroGuid);
                MMDevice defaultDevice = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render‌​, Role.Multimedia);
            }
            catch { Console.WriteLine("VolumeMixer.SetVol casting issue"); }
        }
예제 #11
0
        public static int GetMasterMicVolume()
        {
            IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
            IMMDevice           speakers;
            const int           eCapture    = 1;
            const int           eMultimedia = 1;

            deviceEnumerator.GetDefaultAudioEndpoint(eCapture, eMultimedia, out speakers);

            object aepv_obj;

            speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
            IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
            //Guid ZeroGuid = new Guid();
            //int res = aepv.SetMasterVolumeLevelScalar(level / 100f, ZeroGuid);
            float masterLevel = 0.00f;

            aepv.GetMasterVolumeLevelScalar(ref masterLevel);
            return(Convert.ToInt32(masterLevel * 100));
        }
예제 #12
0
        public static void SetMasterMicVolume(int level)
        {
            if (level < 0 || level > 100)
            {
                return;
            }

            IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
            IMMDevice           speakers;
            const int           eCapture    = 1;
            const int           eMultimedia = 1;

            deviceEnumerator.GetDefaultAudioEndpoint(eCapture, eMultimedia, out speakers);

            object aepv_obj;

            speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
            IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
            Guid ZeroGuid             = new Guid();
            int  res = aepv.SetMasterVolumeLevelScalar(level / 100f, ZeroGuid);
        }
        /// <summary>
        /// Set the volume on the victims pc
        /// </summary>
        /// <param name="percentage">0-100</param>
        private static void SetVolume(string percentage)
        {
            try
            {
                float volume = float.Parse(percentage) / 100;

                keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0);
                keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

                IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
                IMMDevice           speakers;
                const int           eRender     = 0;
                const int           eMultimedia = 1;
                deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers);

                object aepv_obj;
                speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
                IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
                Guid ZeroGuid             = new Guid();
                int  res = aepv.SetMasterVolumeLevelScalar(volume, ZeroGuid);
            }
            catch (Exception) { }
        }
예제 #14
0
        public static void SetApplicationMute(int pid, bool?mute)
        {
            if (mute == null)
            {
                return;
            }
            {
                IMMDeviceEnumerator     deviceEnumerator  = null;
                IAudioSessionEnumerator sessionEnumerator = null;
                IAudioSessionManager2   mgr = null;
                IMMDevice speakers          = null;
                try
                {
                    deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
                    deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);

                    Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
                    speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out object o);
                    mgr = (IAudioSessionManager2)o;

                    mgr.GetSessionEnumerator(out sessionEnumerator);
                    sessionEnumerator.GetCount(out int count);

                    ISimpleAudioVolume volumeControl = null;
                    for (int i = 0; i < count; ++i)
                    {
                        IAudioSessionControl2 ctl = null;
                        try
                        {
                            sessionEnumerator.GetSession(i, out ctl);

                            ctl.GetProcessId(out int cpid);

                            if (cpid == pid)
                            {
                                Guid guid = Guid.Empty;
                                volumeControl = ctl as ISimpleAudioVolume;
                                volumeControl.SetMute(Convert.ToBoolean(mute), ref guid);
                            }
                        }
                        finally
                        {
                            if (ctl != null)
                            {
                                Marshal.ReleaseComObject(ctl);
                            }
                        }
                    }
                }
                catch
                {
                }
                finally
                {
                    if (speakers != null)
                    {
                        Marshal.ReleaseComObject(speakers);
                    }
                    if (mgr != null)
                    {
                        Marshal.ReleaseComObject(mgr);
                    }
                    if (sessionEnumerator != null)
                    {
                        Marshal.ReleaseComObject(sessionEnumerator);
                    }
                    if (deviceEnumerator != null)
                    {
                        Marshal.ReleaseComObject(deviceEnumerator);
                    }
                }
            }
        }
예제 #15
0
        public static void SetApplicationVolume(int?pid, float?volume)
        {
            if (pid == null || volume == null)
            {
                return;
            }
            IMMDeviceEnumerator     deviceEnumerator  = null;
            IAudioSessionEnumerator sessionEnumerator = null;
            IAudioSessionManager2   mgr = null;
            IMMDevice speakers          = null;

            try
            {
                deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
                deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);

                Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
                speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out object o);
                mgr = (IAudioSessionManager2)o;

                mgr.GetSessionEnumerator(out sessionEnumerator);
                sessionEnumerator.GetCount(out int count);

                ISimpleAudioVolume volumeControl = null;
                for (int i = 0; i < count; ++i)
                {
                    IAudioSessionControl2 ctl = null;
                    try
                    {
                        sessionEnumerator.GetSession(i, out ctl);

                        ctl.GetProcessId(out int cpid);

                        if (cpid == pid)
                        {
                            Guid guid = Guid.Empty;
                            volumeControl = ctl as ISimpleAudioVolume;
                            volumeControl.SetMasterVolume((float)volume, ref guid);
                        }
                    }
                    finally
                    {
                        if (ctl != null)
                        {
                            Marshal.ReleaseComObject(ctl);
                        }
                    }
                }
            }
            catch
            {
                Debug.WriteLine("Major Error");
                // If it gets to this point something is very wrong
            }
            finally
            {
                if (speakers != null)
                {
                    Marshal.ReleaseComObject(speakers);
                }
                if (mgr != null)
                {
                    Marshal.ReleaseComObject(mgr);
                }
                if (sessionEnumerator != null)
                {
                    Marshal.ReleaseComObject(sessionEnumerator);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
        }