I’ve been using Discord for gaming and hanging out with friends. However, sometimes I want to use my computer’s speakers (for friend hangouts), and sometimes I want to use headphones (for gaming).

On Windows there’s a handy system tray widget which lets you change the default playback device – and almost everything plays from the default playback device! However, Discord uses the default communications device when set to default… and you have to go into a menu to change that. Very annoying.

Thankfully some kind person on the internet wrote a script to solve this probem:

CommunicationDeviceSwitcherService is a Windows service which switches the default communications device automatically when the playback device is changed from the system tray. I’m not sure why this isn’t the default behavior, but it solves the problem for me.

The actual code is (as expected) very simple:

public void OnDefaultDeviceChanged([MarshalAs(UnmanagedType.I4)] DataFlow dataFlow, [MarshalAs(UnmanagedType.I4)] Role deviceRole, [MarshalAs(UnmanagedType.LPWStr)] string defaultDeviceId)
{
    try
    {
        if (dataFlow == DataFlow.Render && deviceRole == Role.Multimedia)
        {
            var deviceId = defaultDeviceId;
            _policyConfig.SetDefaultEndpoint(deviceId, Role.Communications);
        }
    }
    catch (Exception ex)
    {
        _logger.LogInformation(ex.ToString());
    }
}

Go figure.