Skip to content

Commit f0aade8

Browse files
committed
stop audio proxies before they are stopped by windows sleep
1 parent 75d9243 commit f0aade8

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Project-Aurora/Project-Aurora/Modules/AudioCapture/AudioDeviceProxy.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Runtime.CompilerServices;
5+
using System.Threading;
56
using JetBrains.Annotations;
7+
using Microsoft.Win32;
68
using NAudio.CoreAudioApi;
79
using NAudio.Wave;
810

@@ -41,6 +43,8 @@ public AudioDeviceProxy(string deviceId, DataFlow flow)
4143
_deviceEnumerator.RegisterEndpointNotificationCallback(this);
4244

4345
Instances.Add(this);
46+
47+
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
4448
}
4549

4650
/// <summary>Indicates recorded data is available on the selected device.</summary>
@@ -209,13 +213,30 @@ public void OnPropertyValueChanged(string pwstrDeviceId, PropertyKey key)
209213
//unused
210214
}
211215

216+
#region PowerState
217+
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
218+
{
219+
switch (e.Mode)
220+
{
221+
case PowerModes.Suspend:
222+
DisposeCurrentDevice();
223+
break;
224+
case PowerModes.Resume:
225+
Thread.Sleep(TimeSpan.FromSeconds(2));
226+
UpdateDevice();
227+
break;
228+
}
229+
}
230+
#endregion
231+
212232
#region IDisposable Implementation
213233

214234
private bool _disposed;
215235

216236
public void Dispose()
217237
{
218238
if (_disposed) return;
239+
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
219240
_disposed = true;
220241
Device?.Dispose();
221242
DisposeCurrentDevice();

0 commit comments

Comments
 (0)