3

I was wondering how I can select the output device for audio in directshow. I am able to get available audio output devices in directshow. But how can I make one of these to be audio output device. Its always going for the default audio device. I want to be able to output audio on my choice of device. I have been struggling through google but couldn't find anything useful. All I could get was this link but it doesn't really solve my problem.

Any help will be really helpful for me.

Roman R.
  • 68,205
  • 6
  • 94
  • 158

2 Answers2

4

First off, if you're not using DirectShow .NET (DirectShowLib), get that here: It serves as a (very complete) interface between unmanaged DirectShow and C#

What follows is a pretty simple example of how to play an audio file, to the desired audio device

using DirectShowLib;

private IGraphBuilder m_objFilterGraph = null;
private IBasicAudio m_objBasicAudio = null;
private IMediaControl m_objMediaControl = null;

private void playAudioToDevice(string fName, int devIndex)
{
    object source = null;
    DsDevice[] devices;
    devices = DsDevice.GetDevicesOfCat(FilterCategory.AudioRendererCategory);
    DsDevice device = (DsDevice)devices[devIndex];
    Guid iid = typeof(IBaseFilter).GUID;
    device.Mon.BindToObject(null, null, ref iid, out source);

    m_objFilterGraph = (IGraphBuilder)new FilterGraph();
    m_objFilterGraph.AddFilter((IBaseFilter)source, "Audio Render");
    m_objFilterGraph.RenderFile(fName, "");

    m_objBasicAudio = m_objFilterGraph as IBasicAudio;
    m_objMediaControl = m_objFilterGraph as IMediaControl;

    m_objMediaControl.Run();
}
Digital_Utopia
  • 816
  • 8
  • 17
  • 2
    BTW in this code snippet a weak place is `RenderFile` call. Yes it finds pre-added renderer often, and always in this simple example, however it is more reliable to connect pins directly instead. That is, when the renderer's pin is explicitly being connected. – Roman R. Dec 13 '12 at 23:30
2

It is up to user to manage audio devices and choose a primary device (such as via Control Panel applet). You can find ways to switch devices programmatically in Windows XP, however in Vista+ it is impossible without interactive user action by design.

See also Larry's answer here: How to change default sound playback device programmatically?

UPDATE: The mentioned above refers to modifying system configuration trying to alter default audio output device. An application is however not limited to default device only. Instead, it can enumerate available devices (see Using the System Device Enumerator + CLSID_AudioRendererCategory) and then create an instance of renderer for specific device with BindToObject call. From there on, it is a regular filter, just bound internally to device of interest.

Redoman
  • 3,059
  • 3
  • 34
  • 62
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Personally, I don't see how this was the "answer", as Vibhore never asked how to change which audio device was set as default - with or without user interaction. Rather, the question clearly asks, how to output to an audio device other than the default one. For example, in many games/audio related apps, the user is presented with a listbox containing a list of available playback devices, from which he/she can choose. The chosen device is what the application/game will use for audio output. – Digital_Utopia Dec 13 '12 at 16:15
  • @Digital_Utopia: Good point, now it's hopefully better answer. – Roman R. Dec 13 '12 at 16:54
  • much better;) I added my answer before I saw your update. Since I wound up finally figuring it out for myself, I thought I'd add a snippet from my own project as an example. – Digital_Utopia Dec 13 '12 at 23:11