JL.
JL.

Reputation: 81342

C# How to programatically change the playback device

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

Upvotes: 10

Views: 21348

Answers (3)

Domchix
Domchix

Reputation: 31

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);
}

Upvotes: 2

kwyntes
kwyntes

Reputation: 1302

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;

Upvotes: 1

John Gietzen
John Gietzen

Reputation: 49564

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

Upvotes: 8

Related Questions