Play sound in a specific device using C #

My problem is that I want to create 3 buttons, and I have 3 sound cards, and each button is associated with a sound card (for example, button 1 is associated with a sound card 1 ...), and when I press button 1, I want to hear a sound from the speaker that is connected to the sound card1 (the same for buttons 2 and 3.

My friends gave me two codes: the first use of Naudio it works, but I worked, but I still can’t play the sound in three sound cards, I mean only one sound card when I install three sound cards. This is similar to the fact that allways selects the default sound card from three external sound cards.

The second use of DirectX, and it works for me, but I did not understand how he called the device number, I mean in the code that uses NAudio, for example, "devicenumber = 1", I need to know how because What'll I spicifie device for each button (for example, when I press button1, the sound will play in sound card1)?

I want to know how we can fix one of the two codes, and how can I indicate β€œDevice” in the second code, in which you will help me?

This is form 2 code (from a project using NAudio), you can find out how it defines a device for each button, but unfortunately this causes the mentioned problem:

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;

        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            hideButtons();
             fileName = ("alarm.wav");
             detectDevices();          

        }
        public void hideButtons()
        {

            bttnAudioDevice1.Visible = false;
            bttnAudioDevice2.Visible = false;
            bttnAudioDevice3.Visible = false;
            bttnAudioDevice4.Visible = false;
            bttnAudioDevice5.Visible = false;
        }
        public void detectDevices()
        {
            int 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 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();

                }
            }
        }
    }
}

And this is the Form1 code (from a project using DirectX):

using System;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;
using DirectSound = Microsoft.DirectX.DirectSound;
namespace DirectSoundPlay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ReSizeControls();
        }
        private void playToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItems.Count == 0)
                return;
            if (listBox2.SelectedItems.Count > 1)
            {
                MessageBox.Show("Too many SelectedItems");
                return;
            }
            ClassAudioDevice ad = listBox2.SelectedItem as ClassAudioDevice;
            if (ad == null)
            {
                MessageBox.Show("SelectedItem is not a ClassAudioDevice");
                return;
            }
            DirectSound.Device Device = new DirectSound.Device(ad.DriverGuid);
            Device.SetCooperativeLevel(this.Handle, DirectSound.CooperativeLevel.Priority);
            DirectSound.Buffer AudioBuffer = new DirectSound.Buffer("C:\\Windows\\Media\\notify.wav", Device);
            AudioBuffer.Play(0, BufferPlayFlags.Default);
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            ReSizeControls();
        }
        private void ReSizeControls()
        {
            int w = ClientSize.Width >> 1;
            listBox1.Width = w - 1;
            listBox2.Width = w - 1;
            listBox1.Height = ClientSize.Height;
            listBox2.Height = ClientSize.Height;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            DirectSound.DevicesCollection DevicesList = new DirectSound.DevicesCollection();
            DirectSound.CaptureDevicesCollection CaptureDevicesList = new DirectSound.CaptureDevicesCollection();
            ClassAudioDevice ad;
            //
            foreach (DirectSound.DeviceInformation di in CaptureDevicesList)
            {
                ad = new ClassAudioDevice();
                ad.Description = di.Description;
                ad.DriverGuid = di.DriverGuid;
                listBox1.Items.Add(ad);
            }
            foreach (DirectSound.DeviceInformation di in DevicesList)
            {
                ad = new ClassAudioDevice();
                ad.Description = di.Description;
                ad.DriverGuid = di.DriverGuid;
                listBox2.Items.Add(ad);
            }
        }
    }
}

And this is the ClassAudioDevice code (from the same project):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DirectSoundPlay
{
    class ClassAudioDevice
    {
        public string Description = "";

        public Guid DriverGuid = new Guid();

        public override string ToString()
        {
            return Description;
        }
    }
}

Thanks in advance. A good day.

+5
2

WaveOut, , playSound , IWavePlayer ( DirectSoundOut). ( ), playSound.

public void playSound(int deviceNumber)
{
    disposeWave();// stop previous sounds before starting
    waveReader = new NAudio.Wave.WaveFileReader(fileName);
    var waveOut = new NAudio.Wave.WaveOut();
    waveOut.DeviceNumber = deviceNumber;
    output = waveOut;
    output.Init(waveReader);
    output.Play();
}
+9

, , , (irrKlang), . , , : http://www.ambiera.com/irrklang/downloads.html. , .

//Get the list of installed sound devices. 
sdl = new IrrKlang.ISoundDeviceList(IrrKlang.SoundDeviceListType.PlaybackDevice);

//Add each device to a combo box.
for(int i = 0; i < sdl.DeviceCount; i++)
{
    comboBox1.Items.Add(sdl.getDeviceDescription(i) + "\n");
}


//Place this code in your play sound event handler.
//Create a sound engine for the selected device (uses the ComboBox index to 
//get device ID).
irrKlangEngine = new IrrKlang.ISoundEngine(IrrKlang.SoundOutputDriver.AutoDetect,
                IrrKlang.SoundEngineOptionFlag.DefaultOptions, 
                sdl.getDeviceID(comboBox1.SelectedIndex));

//Play the selected file
playSelectedFile(fileName);

, -.

+3

All Articles