This project has moved. For the latest updates, please go here.

How to use thread to playback sound in two or three devices at the same time

Jun 16, 2011 at 9:49 PM

Hello

Ichanged the NAudio Playback function and now it plays sound in a specific device but I want to playback a sound file in two or three external sound cards at the same time and I think that using threads is the solution but I really didn't know how to use it in the playback code. This is the event makes on button play:

public partial class PlaybackForm : Form
{
    IWavePlayer waveOut;
    string fileName = null;
    WaveStream mainOutputStream;
    WaveChannel32 volumeStream;
    int _deviceNum;
    int _deviceNum1;
    Thread t1;
    Thread t2;
    public PlaybackForm(int deviceNum,int deviceNum1)
    {
        InitializeComponent();
        _deviceNum = deviceNum;
        _deviceNum1 = deviceNum1;
    }


    private void buttonPlay_Click(object sender, EventArgs e)
    {
        if (waveOut != null)
        {
            if (waveOut.PlaybackState == PlaybackState.Playing)
            {
              return;
            }
            else if (waveOut.PlaybackState == PlaybackState.Paused)
            {
                waveOut.Play();
                return;
            }
        }

        // we are in a stopped state
        // TODO: only re-initialise if necessary

        if (String.IsNullOrEmpty(fileName))
        {
            toolStripButtonOpenFile_Click(sender, e);
        }
        if (String.IsNullOrEmpty(fileName))
        {
            return;
        }

        try
        {
            CreateWaveOut();
        }
        catch (Exception driverCreateException)
        {
            MessageBox.Show(String.Format("{0}", driverCreateException.Message));
            return;
        }

        mainOutputStream = CreateInputStream(fileName);
        trackBarPosition.Maximum = (int)mainOutputStream.TotalTime.TotalSeconds;
        labelTotalTime.Text = String.Format("{0:00}:{1:00}", (int)mainOutputStream.TotalTime.TotalMinutes,
            mainOutputStream.TotalTime.Seconds);
        trackBarPosition.TickFrequency = trackBarPosition.Maximum / 30;

        try
        {
            waveOut.Init(mainOutputStream);
        }
        catch (Exception initException)
        {
            MessageBox.Show(String.Format("{0}", initException.Message), "Error Initializing Output");
            return;
        }

        // not doing Volume on IWavePlayer any more
        volumeStream.Volume = volumeSlider1.Volume;
        waveOut.Play();
    }


And this is how to create the waveout:

  
private void CreateWaveOut()
    {
        CloseWaveOut();
        int latency = (int)comboBoxLatency.SelectedItem;
        //if (radioButtonWaveOut.Checked)
        {
            //WaveCallbackInfo callbackInfo = checkBoxWaveOutWindow.Checked ?
            WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
            // WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();

            // WaveCallbackInfo.NewWindow(): WaveCallbackInfo.FunctionCallback();
            WaveOut outputDevice = new WaveOut(callbackInfo);
            outputDevice.DesiredLatency = latency;
            outputDevice.DeviceNumber = _deviceNum;
            waveOut = outputDevice;
        }

    }
Please can you give me an idea?????? it's very important for me.
Thank you in advance.
Good day