Beispiel #1
1
 public AEDev()
 {
   IMMDevice _Device = null;
   if (String.IsNullOrEmpty(_devId))
   {
     Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(0, 1, out _Device));
     Marshal.ThrowExceptionForHR(_Device.GetId(out _devId));
   }
   else
   {
     Marshal.ThrowExceptionForHR(_realEnumerator.GetDevice(_devId, out _Device));
   }
   devstatus state;
   Marshal.ThrowExceptionForHR(_Device.GetState(out state));
   if (state != devstatus.DEVICE_STATE_ACTIVE)
     throw new ApplicationException(String.Format("audio device is not active ({0})", state.ToString()));
   _RealDevice = _Device;
   object result;
   Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result));
   _AudioEndPointVolume = result as IAudioEndpointVolume;
   _CallBack = new AudioEndpointVolumeCallback(this);
   Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
 }
Beispiel #2
0
 public void Dispose()
 {
     if (_CallBack != null)
     {
         try
         {
             Marshal.ThrowExceptionForHR(_AudioEndPointVolume.UnregisterControlChangeNotify(_CallBack));
         }
         catch (Exception)
         {
             // Catch if no device is found
         }
         _CallBack = null;
     }
 }
Beispiel #3
0
 public void Dispose()
 {
   if (_CallBack != null)
   {
     Marshal.ThrowExceptionForHR(_AudioEndPointVolume.UnregisterControlChangeNotify(_CallBack));
     _CallBack = null;
   }
 }
 internal AEDev()
 {
   IMMDevice _Device = null;
   Marshal.ThrowExceptionForHR(((IMMDeviceEnumerator)_realEnumerator).GetDefaultAudioEndpoint(0, 1, out _Device));
   _RealDevice = _Device;
   object result;
   Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result));
   _AudioEndPointVolume = result as IAudioEndpointVolume;
   _CallBack = new AudioEndpointVolumeCallback(this);
   Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
 }
 public void Dispose()
 {
   if (_CallBack != null)
   {
     try
     {
       Marshal.ThrowExceptionForHR(_AudioEndPointVolume.UnregisterControlChangeNotify(_CallBack));
     }
     catch (Exception)
     {
       // Catch if no device is found
     }
     _CallBack = null;
   }
 }
        public AEDev(bool resetDevice = false)
        {
            IMMDevice _Device = null;

            if (resetDevice)
            {
                _devId = "";
            }

            //_realEnumerator = new _AEDeviceEnumerator() as IMMDeviceEnumerator;
            _realEnumerator = _AEDeviceEnumeratorFactory.CreateInstance();
            try
            {
                if (String.IsNullOrEmpty(_devId))
                {
                    Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(0, 1, out _Device));
                    Marshal.ThrowExceptionForHR(_Device.GetId(out _devId));
                }
                else
                {
                    Marshal.ThrowExceptionForHR(_realEnumerator.GetDevice(_devId, out _Device));
                }
                devstatus state;
                Marshal.ThrowExceptionForHR(_Device.GetState(out state));
                if (state != devstatus.DEVICE_STATE_ACTIVE)
                {
                    throw new ApplicationException($"audio device is not active ({state.ToString()})");
                }
                _RealDevice = _Device;
                object result;
                Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result));
                _AudioEndPointVolume = result as IAudioEndpointVolume;
                _CallBack            = new AudioEndpointVolumeCallback(this);
                Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
            }
            catch (Exception)
            {
                // Catch if no device is found or changed device
                try
                {
                    Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(0, 1, out _Device));
                    Marshal.ThrowExceptionForHR(_Device.GetId(out _devId));
                    devstatus state;
                    Marshal.ThrowExceptionForHR(_Device.GetState(out state));
                    if (state != devstatus.DEVICE_STATE_ACTIVE)
                    {
                        throw new ApplicationException($"audio device is not active ({state.ToString()})");
                    }
                    _RealDevice = _Device;
                    object result;
                    Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result));
                    _AudioEndPointVolume = result as IAudioEndpointVolume;
                    _CallBack            = new AudioEndpointVolumeCallback(this);
                    Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
                }
                catch (Exception)
                {
                    // Catch if no device is found
                }
            }
        }