views:

161

answers:

0

I need to be able to direct sound output from my application to all of a system's audio outputs - specifically, both the built-in speakers and headphones of a laptop. I'm using C# on Windows Vista/7 and the latest version of the .NET Framework, and don't need to support older (i.e. Windows XP) OSes.

I was able to successfully use the Core Audio API to change the volume for the active speakers, and was able to enumerate and specifically manipulate individual audio devices, but was unable to actually change the state of the devices to enable them.

private MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
private MMDeviceCollection audioDevices = null;

public Form1()
{
    InitializeComponent();
    this.defaultDevice = this.devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
    AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);
    this.audioDevices = this.devEnum.EnumerateAudioEndPoints(EDataFlow.eRender, EDeviceState.DEVICE_STATEMASK_ALL);
    for (int i = 1; i < this.audioDevices.Count; i++)
    {
        // Doesn't work - throws exception because property is read-only:
        // this.audioDevices[i].State = EDeviceState.DEVICE_STATE_ACTIVE 
    }
}

Basically, what I am looking for is a way to enable audio devices, not just adjust their volume. Does anybody know of a way to do this? I've been looking at the MSDN Documentation for IMMDevice Interface but am finding it difficult since it's not offered as managed code, and the .NET wrapper I'm using doesn't fully implement it. I'm also open to suggestions that take a completely different approach, as long as I can still do it using C#. Thanks!