Exemplo n.º 1
0
        public AudioDevice(IAudioDeviceManager deviceManager, IMMDevice device, Dispatcher foregroundDispatcher)
        {
            _device        = device;
            _deviceManager = new WeakReference <IAudioDeviceManager>(deviceManager);
            _dispatcher    = foregroundDispatcher;
            _id            = device.GetId();

            Trace.WriteLine($"AudioDevice Create {_id}");

            if (_device.GetState() == DeviceState.ACTIVE)
            {
                _deviceVolume = device.Activate <IAudioEndpointVolume>();
                _deviceVolume.RegisterControlChangeNotify(this);
                _deviceVolume.GetMasterVolumeLevelScalar(out _volume);
                _isMuted       = _deviceVolume.GetMute() != 0;
                _isRegistered  = true;
                _meter         = device.Activate <IAudioMeterInformation>();
                _channels      = new AudioDeviceChannelCollection(_deviceVolume, _dispatcher);
                _sessions      = new AudioDeviceSessionCollection(this, _device, _dispatcher);
                _sessionFilter = new FilteredCollectionChain <IAudioDeviceSession>(_sessions.Sessions, _dispatcher);
                Groups         = _sessionFilter.Items;
            }
            else
            {
                Groups = new ObservableCollection <IAudioDeviceSession>();
            }

            ReadProperties();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Switches between the master volume mute states depending on the current state
        /// </summary>
        /// <returns>the current mute state, true if the volume was muted, false if unmuted</returns>
        public static bool ToggleMasterVolumeMute()
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return(false);
                }

                bool isMuted;
                masterVol.GetMute(out isMuted);
                masterVol.SetMute(!isMuted, Guid.Empty);

                return(!isMuted);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public static void listDeviceProperties(IMMDevice dev, DEVICE_SUMMARY defaultDevice)
        {
            IPropertyStore propertyStore;

            dev.OpenPropertyStore(0 /*STGM_READ*/, out propertyStore);
            PROPVARIANT property;  propertyStore.GetValue(ref PropertyKey.PKEY_Device_EnumeratorName, out property);

            System.Console.WriteLine(" EnumeratorName: " + (string)property.Value);
            propertyStore.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out property);
            System.Console.WriteLine(" FriendlyName: " + (string)property.Value);
            if (!defaultDevice.Equals(null) && defaultDevice.FriendlyName == (string)property.Value)
            {
                System.Console.WriteLine(" **Default: True**");
            }
            Marshal.ReleaseComObject(propertyStore);

            IAudioEndpointVolume epv = null;
            var epvid = typeof(IAudioEndpointVolume).GUID;

            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));

            bool mute;  epv.GetMute(out mute);  if (mute)
            {
                System.Console.WriteLine(" **GetMute: " + mute + "**");
            }
            float vol;  epv.GetMasterVolumeLevelScalar(out vol);  System.Console.WriteLine(" GetMasterVolumeLevelScalar: " + vol);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get Mute.
        /// </summary>
        /// <returns></returns>
        public bool GetMuted()
        {
            bool isMute = false;
            var  result = _audioEndpointVolume.GetMute(out isMute);

            return(isMute);
        }
Exemplo n.º 6
0
        public static bool IsVolumeMuted()
        {
            // get the speakers (1st render + multimedia) device
            IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
            IMMDevice           speakers;
            const int           eRender     = 0;
            const int           eMultimedia = 1;

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

            object o;

            speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out o);
            IAudioEndpointVolume aepv = (IAudioEndpointVolume)o;
            int isMuted = aepv.GetMute();

            Marshal.ReleaseComObject(aepv);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);

            if (isMuted == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 7
0
        public void MuteUnmuteSystemVolume()
        {
            bool muteState;

            _aepv.GetMute(out muteState);
            _aepv.SetMute(!muteState, new System.Guid());
        }
Exemplo n.º 8
0
        public Boolean GetMasterMuteState()
        {
            // get the master mute state
            Int32 isMutedAsInt32;
            var   result = _audioEndpointVolume.GetMute(out isMutedAsInt32);

            if (result != WindowsApi.S_OK)
            {
                // TODO: consider throwing more granular exceptions here
                throw new COMException("IAudioEndpointVolume.GetMute failed", Marshal.GetExceptionForHR(result));
            }

            return((isMutedAsInt32 != 0) ? true : false);
        }
Exemplo n.º 9
0
        public static DEVICE_SUMMARY getDefaultDeviceSummary(int dataFlow, int role)
        {
            DEVICE_SUMMARY dev1       = new DEVICE_SUMMARY();  dev1.dataFlow = dataFlow;  dev1.role = role;
            var            enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDevice      dev        = null;  Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(dataFlow, role, out dev));
            IPropertyStore propertyStore;  dev.OpenPropertyStore(0 /*STGM_READ*/, out propertyStore);
            PROPVARIANT    property;  propertyStore.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out property);

            dev1.FriendlyName = (string)property.Value;
            Marshal.ReleaseComObject(propertyStore);
            IAudioEndpointVolume epv = null;
            var epvid = typeof(IAudioEndpointVolume).GUID;

            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
            epv.GetMute(out dev1.isMuted);
            Marshal.ReleaseComObject(dev);
            return(dev1);
        }
Exemplo n.º 10
0
        public AudioDevice(IMMDevice device)
        {
            _device     = device;
            _dispatcher = App.Current.Dispatcher;
            _id         = device.GetId();

            Trace.WriteLine($"AudioDevice Create {_id}");

            _deviceVolume = device.Activate <IAudioEndpointVolume>();

            _deviceVolume.RegisterControlChangeNotify(this);
            _meter    = device.Activate <IAudioMeterInformation>();
            _sessions = new AudioDeviceSessionCollection(this, _device);

            _deviceVolume.GetMasterVolumeLevelScalar(out _volume);
            _isMuted = _deviceVolume.GetMute() != 0;

            ReadDisplayName();
        }
Exemplo n.º 11
0
        public static void MuteMaster()
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();

                var muteState = false;
                masterVol.GetMute(out muteState);
                masterVol.SetMute(!muteState, Guid.Empty);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
Exemplo n.º 12
0
        public static bool MasterMuteState()
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();

                var muteState = false;
                masterVol.GetMute(out muteState);
                return(muteState);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Gets the mute state of the master volume.
        /// While the volume can be muted the <see cref="GetMasterVolume"/> will still return the pre-muted volume value.
        /// </summary>
        /// <returns>false if not muted, true if volume is muted</returns>
        public bool GetMasterVolumeMute()
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return(false);
                }

                bool isMuted;
                masterVol.GetMute(out isMuted);
                return(isMuted);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }