Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4568802
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:07:42+00:00 2026-05-21T19:07:42+00:00

My problem is if I want to create 3 buttons and I have 3

  • 0

My problem is 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…) and when I click on button1 I want to hear sound from the speaker which is related to sound card1 (the same for button2 and button3.

My friends gave me two codes: the first use Naudio it works but worked for I still cant play sound in the three sound cards. I mean only one sound card work when I install the three sound cards. It’s like the program is always choosing a default audio card from the three external sound cards.

The second use DirectX and it works for me but I didn’t understand how he call the device number. I mean in the code which use NAudio there is "devicenumber= 1 for example". I need to know how because I’ll specify a device for each button (for example when I click on button1 the sound will be played in sound card1)?

I want to know how we can correct one of the two codes and how can I specify a "Device " in the second code.

This is the Code of Form2 (from the project which use NAudio) you can notices how it specify a device for each button but unfortunately it cause the problem mentioned:

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 code of Form1 (from the 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 code of ClassAudioDevice (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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:07:43+00:00Added an answer on May 21, 2026 at 7:07 pm

    In your button click handlers you are creating a WaveOut device, setting its device number, and then your playSound function plays sound using a completely different IWavePlayer (an instance of DirectSoundOut). There are several problems with your code as it stands (particularly with concurrent playbacks), but I would start by passing the device number into the playSound function.

    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;
        waveOut.Init(waveReader);
        waveOut.Play();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem: Within a stylesheet document I want to create an
I have a problem with moving animation. I want to create animation which start
So here is the problem: I am trying to create dynamic buttons that have
I have a strange problem. When I want to create a new Spring MVC
I have created a class to play the sound when I click the buttons.
my problem is this: i want to create a server control that inherits System.Web.UI.WebControls.Button
I have a problem with my radio buttons. What I'm doing is I create
In my application I want to create buttons, minus the whole button part. Like
I have problem in Navigation Controller. I can't create dynamic buttons on Navigation Bar.
Problem I want to create an application that can be extended somehow by programmers.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.