10

On my Windows 7 PC, I've got a set of speakers, some wireless headphones and a USB web cam. This means that I have two possible audio output devices and 2 possible audio input devices.

I find myself having to switch between them fairly frequently. At the moment this is a manual process: right-click on the speaker icon, choose one of "Playback devices" or "Recording devices", choose the correct device in the list (and there's some "dead" ones in there, too) and then hit "Set Default".

I've looked around, and all I can find are people scripting SendKeys to automate this.

That sucks.

Is there anyway to programmatically switch audio input/output devices, so that I can write a simple tray app/hotkey app to make this easier?

Roman R.
  • 68,205
  • 6
  • 94
  • 158
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
  • http://superuser.com/questions/201973/easy-way-to-switch-default-sound-output-device They link to SoundSwitch on Codeplex which seems to work. I haven't looked through the code enough to tell you which parts you need to reproduce though. – TankorSmash Jun 14 '15 at 23:18

2 Answers2

16

Allegedly undocumented COM-interface IPolicyConfig (kudos to @author EreTIk) allows to do that.

This is a sample implementation.

HRESULT SetDefaultAudioPlaybackDevice(LPCWSTR devID)
{
    IPolicyConfigVista *pPolicyConfig;
    ERole reserved = eConsole;

    HRESULT hr = CoCreateInstance(
                    __uuidof(CPolicyConfigVistaClient),
                    NULL, 
                    CLSCTX_ALL, 
                    __uuidof(IPolicyConfigVista), 
                    (LPVOID *)&pPolicyConfig);

    if (SUCCEEDED(hr))
    {
        hr = pPolicyConfig->SetDefaultEndpoint(devID, reserved);
        pPolicyConfig->Release();
    }

    return hr;
}

A string of Device ID needs to be passed to this function. An example of a device id

{0.0.1.00000000}.{d915c7bb-d5d7-4c92-80d9-1a0ee5d954f1}

This device id can be obtained through audio device enumeration.

user2529145
  • 265
  • 3
  • 5
  • @RogerLipscombe are you sure? -- Upon looking at the source code I can't find the word "Policy" or "SetDefaultEndpoint" anywhere. -- Which makes me think it's doing something different. – BrainSlugs83 Jun 11 '16 at 01:16
3

If you are looking into changing default devices programmatically, then this is impossible by design.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158