C# How to programatically change the playback device
Asked Answered
P

3

10

How can I programatically change the default audio device on a vista / win 7 system? Using C# or a Win API call?

Pages answered 12/10, 2009 at 15:14 Comment(1)
This is basically a duplicate of #1334576 except input vs output device, either way, there is no API for it on Vista+ unless you want to decompile media center and find the undocumented stuffAnnotate
Q
8

The WinMM API should provide the functionality that you request.

You would use the DRVM_MAPPER_PREFERRED_SET message, which is sent with waveOutMessage() function.

Documentation: http://msdn.microsoft.com/en-us/library/aa909789.aspx

However, if you are trying to send the waveform sound out yourself, you should look at the WinMM.Net library.

http://winmm.codeplex.com

Quan answered 12/10, 2009 at 15:18 Comment(7)
All I need to do is change the sound device on Windows, so that all audio routes through that device. Essentially I have a lot of sound devices on my system, and I want to replace the default sys tray app, so that I don't need to right click , open the device manager each time I need to switch a device. Will this API do this?Pages
Yes, You can set the default audio playback device in XP with the DRVM_MAPPER_PREFERRED_SET message, which is sent with waveOutMessage(). This doesn't work in Vista. I am researching.Quan
Thanks, I think this last link will work, it should port to win 7 hopefully!Pages
Love that last link! It's been driving nuts in Win7 having to launch the Playback devices dialog every time I want to switch from headphones to speakers, and vice-versa. Much easier now. Thanks!Repute
@Repute how exactly did you do that ? WinMM.net doesn't seem to have waveOutMessage functionality. In fact native library import for waveOutMessage is surrounded with #if false preprocessor directive.Landonlandor
Jonathan Sampson removed the link to vistaaudiochanger.com because it's inactive. That sucks. I didn't play with WinMM.net.Repute
In fact DRVM_MAPPER_PREFERRED_SET doesn't work even on XP, it returns MMSYSERR_INVALPARAM. And documentation that you linked to clearly states it's for Windows Mobile.Diophantus
C
2

I found the AudioSwitcher constructor to be extra slow in my case.

I suggest using the "CoreAudio" API for managing audio devices.

Here is a current wrapper library available on NuGet. https://github.com/morphx666/CoreAudio/tree/master

Set device:

public static void SetDefaultDevice(string id) {
    MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator(Guid.NewGuid());
    MMDevice device = deviceEnum.GetDevice(id);
    deviceEnum.SetDefaultAudioEndpoint(device);
}

Get devices

public static MMDeviceCollection GetAudioDevices() {
    MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator(Guid.NewGuid());
    return deviceEnum.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
}
Cashier answered 7/6, 2023 at 21:4 Comment(0)
F
1

This can now (actually for quite some time already) be done very easily using the AudioSwitcher.AudioApi.CoreAudio NuGet package.

Simply create a new CoreAudioController:

var controller = new AudioSwitcher.AudioApi.CoreAudio.CoreAudioController();

Get hold of the desired device using its GUID:

var device = controller.GetDevice(Guid.Parse(...));

And lastly set it as the default playback device:

controller.DefaultPlaybackDevice = device;
Footsie answered 3/10, 2022 at 22:11 Comment(3)
I get error CS0200: Property or indexer 'AudioController<CoreAudioDevice>.DefaultPlaybackDevice' cannot be assigned to -- it is read onlySporophyte
Okay so the API has changed but I can't get playbackDevice.SetAsDefault() to work either. Always returns false.Sporophyte
@KenNetherland I'd suggest you open an issue on their GitHub page then, StackOverflow comments are usually not the best place to find answers to problems like these ;)Footsie

© 2022 - 2024 — McMap. All rights reserved.