nicole castel

nicole castel

  • NA
  • 208
  • 47.8k

how to use thread to playback sound in two or three external sound cards simultaneously

Jun 15 2011 1:10 PM


Hello

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;
       
}

   
}

I declared two deviceNum but until now I can playsound only in one device,that's why I want to use thread. Can you help me please Thank you in advance