Search code examples
c#windowswinapiaudioaudio-streaming

How to stream audio to a specific output device?


Have been struggling with finding a way to stream audio, from a file or web, to a specific output device, not just the default one. Tried using mciSendString and while the open command does accept a device id/filename I haven't found a way to make use of it, am not even sure if this is what I am looking for or not, but considering it says ... or the filename of the device driver am guessing yes(?), but correct me if I am wrong, and this isn't a specify your output device type parameter.

If it is the correct thing then how do you enumerate the installed device drivers, have looked into the IMMDevice interface because it seamed like it could have the file names stored in the registry, but non of the output device registry keys had a driver filename type value entry, or at least I haven't found one.

So my question is, how would you go about streaming audio to a specific output device, it doesn't have to be done through mciSendString, that's just something I looked into as it's one of the most talked about function when it comes to playing audio.

Note: please do not recommend me 3rd party libraries like NAudio, the reason I am asking this question is not get recommendations for libraries, otherwise I would have already used one and would have never written this, have just seen a lot of answers be like: Use {LibName}, it has what you want or something along those lines.

In case what's written is odd or incorrectly worded in places, basically this is what the end goal should be:

Installed Output Devices:
 - Output1
 - Output2
 - Output3

Method For Playing:
  //will play x.mp3 through output device 1
  PlayAudio(output: "Output1", mp3File: "x.mp3");

  //will play x.mp3 through output device 2
  PlayAudio(output: "Output2", mp3File: "x.mp3");

  //will play x.mp3 through output device 3
  PlayAudio(output: "Output3", mp3File: "x.mp3");

Solution

  • You seem to be looking for this API:mciSendCommand()

    To set the WaveAudio device (soundcard) used by the Multimedia Control, you must use the mciSendCommand API. The Multimedia Control does not directly provide a method to let you set the device used for playing or recording.

    • Call mciSendCommand() with MCI_SET & MCI_WAVE_SET_PARMS setting wOutput to the desired playback device's ID.
    • Then get IDDevice for mciSendCommand() via mciGetDeviceID("waveaudio")

    Its not 100% clear what wOutput wants, its probably the same ID as returned by waveOutGetDevCaps()

    I am just a porter.

    Please refer:

    https://stackoverflow.com/a/13320137/11128312

    https://stackoverflow.com/a/10968887/11128312