public void Dispose()
 {
     try
     {
         _device.AudioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification;
         _device = null;
     }
     catch (Exception exc)
     {
         Console.WriteLine("Problem disposing Sound Device " + exc.Message);
     }
     finally
     {
         if (_session != null)
         {
             _session.UnRegisterEventClient(this);
             _session = null;
             GC.Collect();
         }
     }
 }
Example #2
0
        public void Initialize(int rate = -1) // !!! Must be called from the same thread that was initialized
        {
            if (uiThread.InvokeRequired)
            {
                uiThread.BeginInvoke(new Action(() => Initialize(rate))); return;
            }

            lock (locker)
            {
                if (rate != -1)
                {
                    Rate = rate;
                }

                // Dispose | UnRegister
                if (device != null)
                {
                    device.AudioEndpointVolume.OnVolumeNotification -= OnMasterVolumeChanged;
                }
                if (session != null)
                {
                    session.UnRegisterEventClient(this);
                }
                if (player != null)
                {
                    player.Dispose();
                }
                if (buffer != null)
                {
                    buffer.ClearBuffer();
                }
                if (session != null)
                {
                    session.UnRegisterEventClient(this);
                }

                // Initialize
                format = WaveFormatExtensible.CreateIeeeFloatWaveFormat(Rate, CHANNELS);
                buffer = new BufferedWaveProvider(format);
                buffer.BufferLength = 1000 * 1024;
                player = new WaveOut();
                player.DeviceNumber   = 0;
                player.DesiredLatency = NAUDIO_DELAY_MS;
                player.Init(buffer);
                player.Volume = 1; // Currently we change only Master volume to achieve constants change levels
                player.Play();

                device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
                device.AudioEndpointVolume.OnVolumeNotification += OnMasterVolumeChanged;

                for (int i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
                {
                    if (processId == device.AudioSessionManager.Sessions[i].GetProcessID) // && !deviceInit.AudioSessionManager.Sessions[i].IsSystemSoundsSession)
                    {
                        session       = device.AudioSessionManager.Sessions[i];
                        player.Volume = session.SimpleAudioVolume.Volume;
                        session.RegisterEventClient(this);
                    }
                }

                VolumeChanged?.BeginInvoke(this, new VolumeChangedArgs((int)(player.Volume * device.AudioEndpointVolume.MasterVolumeLevelScalar * 100), (session != null ? session.SimpleAudioVolume.Mute : false) || device.AudioEndpointVolume.Mute), null, null);
            }
        }