Esempio n. 1
0
 private void ClearAudioEndpointVolume()
 {
     if (_audioEndpointVolume != null)
     {
         _audioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification;
         _audioEndpointVolume.Dispose();
         _audioEndpointVolume = null;
     }
 }
Esempio n. 2
0
        private void GetAudioEndpointVolume(IMMDevice device)
        {
            //Prevent further look ups
            if (_audioEndpointVolumeUnavailable)
            {
                return;
            }

            //Don't even bother looking up volume for disconnected devices
            if (State == DeviceState.NotPresent || State == DeviceState.Unplugged)
            {
                return;
            }

            //This should be all on the COM thread to avoid any
            //weird lookups on the result COM object not on an STA Thread
            ComThread.Assert();

            object    result = null;
            Exception ex;

            //Need to catch here, as there is a chance that unauthorized is thrown.
            //It's not an HR exception, but bubbles up through the .net call stack
            try
            {
                var clsGuid = new Guid(ComIIds.AUDIO_ENDPOINT_VOLUME_IID);
                ex = Marshal.GetExceptionForHR(device.Activate(ref clsGuid, ClsCtx.Inproc, IntPtr.Zero, out result));
            }
            catch (Exception e)
            {
                ex = e;
            }
            _audioEndpointVolumeUnavailable = ex != null;
            if (_audioEndpointVolumeUnavailable)
            {
                return;
            }
            _audioEndpointVolume = new AudioEndpointVolume(result as IAudioEndpointVolume);
        }
        private void LoadAudioEndpointVolume()
        {
            //Don't even bother looking up volume for disconnected devices
            if (!State.HasFlag(DeviceState.Active))
            {
                ClearAudioEndpointVolume();
                return;
            }

            //This should be all on the COM thread to avoid any
            //weird lookups on the result COM object not on an STA Thread
            ComThread.Assert();

            object    result = null;
            Exception ex;

            //Need to catch here, as there is a chance that unauthorized is thrown.
            //It's not an HR exception, but bubbles up through the .net call stack
            try
            {
                var clsGuid = new Guid(ComInterfaceIds.AUDIO_ENDPOINT_VOLUME_IID);
                ex = Marshal.GetExceptionForHR(Device.Activate(ref clsGuid, ClassContext.Inproc, IntPtr.Zero, out result));
            }
            catch (Exception e)
            {
                ex = e;
            }

            if (ex != null)
            {
                ClearAudioEndpointVolume();
                return;
            }

            _audioEndpointVolume = new AudioEndpointVolume(result as IAudioEndpointVolume);
            _isMuted             = _audioEndpointVolume.Mute;
            _volume = _audioEndpointVolume.MasterVolumeLevelScalar.DeNormalizeVolume();
        }
 internal AudioEndpointVolumeCallback(AudioEndpointVolume handler)
 {
     _handler = new WeakReference(handler);
 }
 internal AudioEndpointVolumeCallback(AudioEndpointVolume handler)
 {
     _handler = new WeakReference(handler);
 }
        private void LoadAudioEndpointVolume(IMMDevice device)
        {
            //Don't even bother looking up volume for disconnected devices
            if (!State.HasFlag(DeviceState.Active))
            {
                ClearAudioEndpointVolume();
                return;
            }

            //This should be all on the COM thread to avoid any
            //weird lookups on the result COM object not on an STA Thread
            ComThread.Assert();

            object result = null;
            Exception ex;
            //Need to catch here, as there is a chance that unauthorized is thrown.
            //It's not an HR exception, but bubbles up through the .net call stack
            try
            {
                var clsGuid = new Guid(ComIIds.AUDIO_ENDPOINT_VOLUME_IID);
                ex = Marshal.GetExceptionForHR(device.Activate(ref clsGuid, ClsCtx.Inproc, IntPtr.Zero, out result));
            }
            catch (Exception e)
            {
                ex = e;
            }

            if (ex != null)
            {
                ClearAudioEndpointVolume();
                return;
            }

            _audioEndpointVolume = new AudioEndpointVolume(result as IAudioEndpointVolume);
        }
 private void ClearAudioEndpointVolume()
 {
     if (_audioEndpointVolume != null)
     {
         _audioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification;
         _audioEndpointVolume.Dispose();
         _audioEndpointVolume = null;
     }
 }