How to play sound in an external sound card

Apr 26, 2011 at 2:21 PM

Hello :)

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 .

Thank you in advance 

Coordinator
Apr 26, 2011 at 2:29 PM

have a look at the NAudioDemo application and you will see how you can select which output device you should play to

Mark

Apr 26, 2011 at 2:36 PM

Thank you for replying, I'm a beginner in Csharp and I cant understand such codes very well,can you give me more details please? Can you give me the xode of the function whichcan play sound in a specific sound card?

Coordinator
Apr 26, 2011 at 2:42 PM

For example, when you use WaveOut you can specify the DeviceNumber. (0 is the default device).

So to select device 3:

var waveOut = new WaveOut();
waveOut.DeviceNumber = 3;
waveOut.Init(...

To find out which device number is which output, you call WaveOut.GetCapabilities(deviceNumber). deviceNumber must be less than WaveOut.DeviceCount

 

Apr 26, 2011 at 7:43 PM

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 .

Apr 27, 2011 at 3:32 PM

where is the problem if you want to select soundcard number 1 WaveOut.DeviceNumer = 1; ... if you want to select number 2 WaveOut.DeviceNumer = 2;

Apr 28, 2011 at 12:06 PM

Thank you for replying but I really didn't understand how can I use this function,sorry for bothering but please give me a clear code,how can I specify to each button in which sound card it should play sound.

Thank you in advance.

Apr 28, 2011 at 2:34 PM

The audio device I/O API are all same except ASIO, no matter the internal or external devices
just depends on the OS, and the ASIO is depends on the spec. of every devices whatever int. or ext.

Apr 28, 2011 at 2:40 PM

So how can I select a device ? I read need to see the code and to try it to understand it

Coordinator
Apr 28, 2011 at 2:43 PM

To select device 1:

var waveOut = new WaveOut();
waveOut.DeviceNumber = 1;

To select device 2:

var waveOut = new WaveOut();
waveOut.DeviceNumber = 2;

To select device 3:

var waveOut = new WaveOut();
waveOut.DeviceNumber = 3;

It's that simple.

Apr 28, 2011 at 2:53 PM

So if I want to play a sound in card sound number 1 i should do this? :

 

namespace alarmee{

    public partial class Form1 : Form   {

     public Form1()    

   {            InitializeComponent(); 

    }      

 [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true, CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true)]        private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);

        [System.Flags]        public enum PlaySoundFlags : int        {            SND_SYNC = 0x0000,            SND_ASYNC = 0x0001,            SND_NODEFAULT = 0x0002,            SND_LOOP = 0x0008,            SND_NOSTOP = 0x0010,            SND_NOWAIT = 0x00002000,            SND_FILENAME = 0x00020000,            SND_RESOURCE = 0x00040004        }

System.Media.SoundPlayer sp = new SoundPlayer();

  private void button1_Click(object sender, EventArgs e)        {
            //Dans ton constructeur :                  

   var waveOut = new WaveOut();          

 waveOut.DeviceNumber = 1;
  string chemin = "..//..//Sequences//alarme.wav";

  sp.SoundLocation = chemin;  

         sp.LoadCompleted += new AsyncCompletedEventHandler(sp_LoadCompleted);            

sp.LoadAsync();      

  }

    //Puis      

 private void sp_LoadCompleted(object sender, AsyncCompletedEventArgs e)        {      

     sp.PlayLooping();      

 }

}}

 

It doesn't work .

Coordinator
Apr 28, 2011 at 2:57 PM

this is an NAudio forum. Your code doesn't use NAudio at all. I don't think the Windows PlaySound API will let you select a soundcard.

Apr 28, 2011 at 3:06 PM

So how can I solve this problem please?

Coordinator
Apr 29, 2011 at 8:30 AM

OK here's how you play a WAV file in NAudio

private WaveOut waveOut;

waveOut = new WaveOut();
waveOut.DeviceNumber = 0; // for default device
var reader = new WavFileReader("my-wav-file.wav");
waveOut.Init(reader);
waveOut.Play();

n.b. make waveOut a class member as Play is not a blocking call. Remember to Dispose it when you are finished with it.

Apr 29, 2011 at 2:42 PM

Thank you very much for your help,now I can play mu soun file in a specific device :)

May 2, 2011 at 10:45 PM

Hello 

A freind gave me this code (This is the code of Form2 wich allows me to redirect the sound for the specific device) but when I install the three sound cards together only one sound card is able to play sound in her speakers but when I install them separetly they works :( can you help me to find a solution please????

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.CoreAudioApi;

namespace AudioWithNAudio
{
    public partial class Form2 : Form
    {
        string fileName = null;
        WaveOut wave = null;
        private NAudio.Wave.WaveFileReader waveReader = null;
        private NAudio.Wave.DirectSoundOut output = null;
        int waveOutDevices = 0;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            hideButtons();
             fileName = ("alarm.wav");
             //bttnPlayAll.Enabled = false;
             detectDevices();          
             
        }

        public void hideButtons()
        {
		
            bttnAudioDevice1.Visible = false;
            bttnAudioDevice2.Visible = false;
            bttnAudioDevice3.Visible = false;
            bttnAudioDevice4.Visible = false;
            bttnAudioDevice5.Visible = false;
        }

        public void detectDevices()
        {
             waveOutDevices = WaveOut.DeviceCount;
            switch (waveOutDevices)
            {
                case 1:
                    bttnAudioDevice1.Visible = true;
                    break;
                case 2:
                    bttnAudioDevice2.Visible = true;
                    bttnAudioDevice1.Visible = true;
                    break;
                case 3:
                      bttnAudioDevice2.Visible = true;
                    bttnAudioDevice1.Visible = true;
                    bttnAudioDevice3.Visible = true;
                    break;
                case 4:
                         bttnAudioDevice2.Visible = true;
                    bttnAudioDevice1.Visible = true;
                    bttnAudioDevice3.Visible = true;
                    bttnAudioDevice4.Visible = true;
                    break;
                case 5:
                            bttnAudioDevice2.Visible = true;
                    bttnAudioDevice1.Visible = true;
                    bttnAudioDevice3.Visible = true;
                    bttnAudioDevice4.Visible = true;
                    bttnAudioDevice5.Visible = true;
                    break;
            }
        }

        private void bttnAudioDevice1_Click(object sender, EventArgs e)
        {
            wave = new WaveOut();
            wave.DeviceNumber = 0;
            playSound();

        }

        private void bttnAudioDevice2_Click(object sender, EventArgs e)
        {
            wave = new WaveOut();
            wave.DeviceNumber = 1;
            playSound();
        }

        private void bttnAudioDevice3_Click(object sender, EventArgs e)
        {
            wave.DeviceNumber = 2;
            playSound();
        }

        private void bttnAudioDevice4_Click(object sender, EventArgs e)
        {
            wave.DeviceNumber = 3;
            playSound();
        }

        private void bttnAudioDevice5_Click(object sender, EventArgs e)
        {
            wave.DeviceNumber = 4;
            playSound();
        }

        public void playAllAvailableDevices()
        {
             wave = new WaveOut();
             switch (waveOutDevices)
             {
                 case 1:

                     wave.DeviceNumber = 0;
                     playAllSound();
                     break;
                 case 2:
                     wave.DeviceNumber = 0;
                     playAllSound();
                     wave.DeviceNumber = 1;
                     playAllSound();
                     break;
                 case 3:
                     wave.DeviceNumber = 0;
                     playAllSound();
                     wave.DeviceNumber = 1;
                     playAllSound();
                     wave.DeviceNumber = 2;
                     playAllSound();
                     break;
                 case 4:
                     wave.DeviceNumber = 0;
                     playAllSound();
                     wave.DeviceNumber = 1;
                     playAllSound();
                     wave.DeviceNumber = 2;
                     playAllSound();
                     wave.DeviceNumber = 3;
                     playAllSound();
                     break;
                 case 5:
                     wave.DeviceNumber = 0;
                     playAllSound();
                     wave.DeviceNumber = 1;
                     playAllSound();
                     wave.DeviceNumber = 2;
                     playAllSound();
                     wave.DeviceNumber = 3;
                     playAllSound();
                     wave.DeviceNumber = 4;
                     playAllSound();
                     break;
             }
        }

        public void playAllSound()
        {
           
            waveReader = new NAudio.Wave.WaveFileReader(fileName);
            output = new NAudio.Wave.DirectSoundOut();

            output.Init(new NAudio.Wave.WaveChannel32(waveReader));
            output.Play();
        }

        public void playSound()
        {
            disposeWave();// stop previous sounds before starting
            waveReader = new NAudio.Wave.WaveFileReader(fileName);
            output = new NAudio.Wave.DirectSoundOut();
           
            output.Init(new NAudio.Wave.WaveChannel32(waveReader));
            output.Play();
        }

        public void disposeWave()
        {
            if (output != null)
            {
                if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                {
                    output.Stop();
                    output.Dispose();
                    output = null;
                }
            }
            if (wave != null)
            {
                wave.Dispose();
                wave = null;
            }
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            disposeWave();
        }

        private void bttnStop_Click(object sender, EventArgs e)
        {
            if (output != null)
            {
                if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                {
                    output.Stop();
                 
                }
            }
        }

        private void bttnPlayAll_Click(object sender, EventArgs e)
        {
            playAllAvailableDevices();
        }
    }
}