Search code examples
c#winapiaudioaudio-recording

How to Change Speaker Configuration in Windows in C#?


I am aware of this old thread: What APIs exist?, but it really didn't answer the question. And it has been a number of years. Yes I am using the NAudio.CoreAudioApi But I'm not finding any useful information.

MMDevice.Properties is readonly. Is there a way to do this programmatically in C#? I'm no longer sure.

You can also find the channels with: AudioEndpointVolumeChannels, but it only allows Channels.count.

Another solution I thought of is with some sort of 'Macro' that changes with mouse-click movements, but that's pretty ugly.

That NAudio API, should have the right-stuff, but I'm not finding any documentation there-in on how to do it. I've googled for like an entire day and found nothing. The old CoreAPIs were moved in there.

using NAudio.Wave;
using NAudio.CoreAudioApi;

        //Can't do anything with these Devices, but change the volume????!!!
        var deviceEnum = new MMDeviceEnumerator();
        var devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active).ToList();
        foreach (MMDevice device in devices)
        {
            Console.WriteLine(device.FriendlyName);

        }

Solution

  • The method that Eugene and I found that worked was to find the Playback devices Registry -- 'Render' That is: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Aud‌​io\Render Then forward slash {Guid}... Your playback device. Make sure your device is in 5.1 or greater mode.

    Then 'Export' that to a file. When you need to restore to 5.1 or greater, which would also include the 'Sample Rate'. Then in code use the following from the exported file:

    Process regeditProcess = Process.Start("regedit.exe", "/s playback.reg");
    regeditProcess.WaitForExit();
    

    Which will make sure the keys are properly restored. Its still not the best way I would like to see. But it certainly works.