0

I'm developping in an application in Csharp and I want to read a sound file in an external sound card,is there a function which give me the possibility to play sound using the name of the sound card and the name of the audio file . So if I want to create 3 buttons and I have 3 sound cards and each button is related to a sound card (for example button1 is related to sound card1 ...) which code can help me to play an audio file in the specific sound card ??? Please give me a code,because I'm preparing a general alarm and such code will help me alot.

Thank you in advance

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
nicolecastel
  • 145
  • 1
  • 2
  • 9
  • Hmm, I don't believe there's a way to target output to a specific audio device on-the-fly. You may find my answer to [this question](http://stackoverflow.com/questions/5675786/how-can-i-notify-my-application-that-the-default-sound-playback-device-has-change) interesting. It accomplishes something similar to what you are trying to achieve, albeit in a somewhat more roundabout manner. (It's *extremely* unusual that one computer has more than one sound card, much less three.) – Cody Gray - on strike Apr 27 '11 at 11:22
  • Also see [this question for Windows XP](http://stackoverflow.com/questions/4115667/change-playback-device-programatically-xp/4117384#4117384), and [this one for Windows Vista and later](http://stackoverflow.com/questions/1555156/c-how-to-programatically-change-the-playback-device). – Cody Gray - on strike Apr 27 '11 at 11:24
  • @Cody Gray: It's not that uncommon. Even a single low-end/on-board sound card often allows switching to more than one output device (e.g. S/PDIF vs Speakers). USB headsets are quite common also. – Ben Schwehn Apr 27 '11 at 14:09

1 Answers1

2

I'm not aware of a way of doing this within .net framework. But you can use the direct sound api:

DirectSoundEnumerate to find available devices and then use the guid of the desired device in DirectSoundCreate.

Please, do not change the default device of the PC by messing around with the registry (or at all). This would be a major annoyance for your users.

Here's some sample code for playing a sound in DirectX natively.

You could check out Managed DirectX and/or XNA. It may make life easier for you by providing you with a managed wrapper for DirectSound.

Ben Schwehn
  • 4,505
  • 1
  • 27
  • 45
  • @Cody Gray,@Ben Schwehn:Thank you very much for replying but yesterday I found a solution with NAudio and i think that it will work. – nicolecastel Apr 29 '11 at 14:56
  • Thanks for the feedback. Didn't know NAudio before! – Ben Schwehn Apr 29 '11 at 16:00
  • @nicolecastel how can i achieve http://stackoverflow.com/questions/38240612/how-transfer-system-mic-audio-stream-to-attached-device-mic-audio-stream?noredirect=1#comment63908020_38240612 this to xchange sound between two sound cards. – Hot Cool Stud Jul 08 '16 at 03:17