Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround to File-New-Project/EarTrumpet#1305 (part of) #1629

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions EarTrumpet/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ private void ContinueStartup()
// Initialize the FlyoutWindow last because its Show/Hide cycle will pump messages, causing UI frames
// to be executed, breaking the assumption that startup is complete.
FlyoutWindow.Initialize();

// listen for user session change
// When user come back after user switch, do some workaround for issue of losing audio sessions
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
Exit += (_, __) => SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
}

private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
Trace.WriteLine($"Detected User Session Switch: {e.Reason}");
if (e.Reason == SessionSwitchReason.ConsoleConnect)
{
var devManager = WindowsAudioFactory.Create(AudioDeviceKind.Playback);
devManager.RefreshAllDevices();
}
}

private void CompleteStartup()
Expand Down
1 change: 1 addition & 0 deletions EarTrumpet/DataModel/Audio/IAudioDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface IAudioDeviceManager
string Kind { get; }
void UpdatePeakValues();
void AddFilter(Func<ObservableCollection<IAudioDevice>, ObservableCollection<IAudioDevice>> filter);
void RefreshAllDevices();
}
29 changes: 29 additions & 0 deletions EarTrumpet/DataModel/WindowsAudio/Internal/AudioDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,33 @@ private void TraceLine(string message)
{
System.Diagnostics.Trace.WriteLine($"AudioDeviceManager-({_kind}) {message}");
}

public void RefreshAllDevices()
{
foreach (var dev in Devices.ToArray())
{
((IMMNotificationClient)this).OnDeviceRemoved(dev.Id);
}
_default = null;

_enumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator();
_enumerator.RegisterEndpointNotificationCallback(this);

_enumerator.EnumAudioEndpoints(Flow, DEVICE_STATE.DEVICE_STATE_ACTIVE, out var devices);
devices.GetCount(out var deviceCount);
for (uint i = 0; i < deviceCount; i++)
{
devices.Item(i, out var device);
device.GetId(out var deviceId);
((IMMNotificationClient)this).OnDeviceAdded(deviceId);
}
this.OnDefaultDeviceChanged(EDataFlow.eRender, ERole.eMultimedia, null);
this.OnDefaultDeviceChanged(EDataFlow.eRender, ERole.eConsole, null);

_dispatcher.Invoke((Action)(() =>
{
QueryDefaultDevice();
}));

}
}