Programmatically changing the default audio playback device on Windows 7

If you’ve ever wanted to programmatically change the default audio playback device on the fly using code, a good friend of mine wrote a great article and subsequent code example (in Win32 C++) that compiles down to a very handy console-based executable (download link at the very bottom of the article) which, if left parameterless, will output all playback devices with an ID number, or, if an ID number is provided as a parameter, will set the device of that ID number to be the default playback device, immediately.

Now, a console application is all well and good, but the real power lies in the ability to wrap this handy utility in a .NET program. Here’s a small code snippet that will let you execute the utility EXE from within a C# program. Just make sure you have the utility EXE in the current working directory (add it to your project and set it to copy to the output folder), or place it in some other folder in your PATH environment variable.

int newDeviceID;

// Set newDeviceID here, by some means. A common scenario
// is to save the value using the registry so it can be toggled.

Process.Start(new ProcessStartInfo("EndPointController.exe", newDeviceID.ToString())
{
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true
});

Simply populate the “newDeviceID” integer with a new value (either store a toggle in the registry or through some other means), ensure that you’ve included the utility EXE (default name is “EndPointController.exe”) and copied it to the output directory, then run this code. The Process options set in the example prevent a console window from appearing, which makes this code completely silent.

So there you have it. A simple, easy way to change your default audio device on the fly, almost instantaneously, through .NET.

14 Responses to "Programmatically changing the default audio playback device on Windows 7"

  • Sabir says:
    • SpikeX says:
      • MVAT says:
  • Jeppe says:
    • SpikeX says:
  • Seffles says:
  • Marcus says:
  • JLuis says:
  • Allan says:
  • Jonas says:
  • Marcus says:
Leave a Comment