public void Initialize() { //lock (locker) //{ //format = new WaveFormatExtensible(_RATE, _BITS, _CHANNELS); format = WaveFormatExtensible.CreateIeeeFloatWaveFormat(_RATE, _CHANNELS); buffer = new BufferedWaveProvider(format); buffer.BufferLength = 3000 * 1024; player = new WaveOut(); player.DeviceNumber = 0; player.DesiredLatency = NAUDIO_DELAY_MS; player.Init(buffer); player.Play(); if (device != null) { device.AudioEndpointVolume.OnVolumeNotification -= OnMasterVolumeChanged; } device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); device.AudioEndpointVolume.OnVolumeNotification += OnMasterVolumeChanged; //} }
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); } }
/// <summary> /// Allows you to specify the sample rate and channels for this WaveProvider /// (should be initialised before you pass it to a wave player) /// </summary> public void SetWaveFormat(int sampleRate, int channels) { this.waveFormat = WaveFormatExtensible.CreateIeeeFloatWaveFormat(sampleRate, channels); }