Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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
Posted 15-Jun-11 7:09am
Comments
SAKryukov at 15-Jun-11 15:38pm
   
Interesting question, my 5, but I would prefer to know the goal of all this activity :-)
--SA
Nicole castel at 17-Jun-11 17:00pm
   
Thank you :) here I want to broadcast a vocal message in two areas at the same time,each area is related to a sound card so it's important how to play the same audio file in different devices at the same time

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Sorry, I don't know the solution but trying to answer. I only want to point out that you might not looking in a right direction.
 
Here is why: even if you achieve that (I hope this is quite possible), I don't think you will be able to synchronize the sounds. Thread synchronization in a non-realtime system (all systems I know which can run .NET or Mono are not real-time systems) can never guarantee "detailed" synchronization of threads. Only the order of execution of few discreet points of the code could be guaranteed. For audio, synchronization is critically important.
 
Probably you would be better of mixing several audio sources into one and playing it on a single audio card. I don't know your ultimate goal though. Perhaps you will deliver the sound to several independent human listeners; then my advice is not applicable.
 
Best,
—SA
  Permalink  
Comments
Nicole castel at 22-Jun-11 15:50pm
   
than kou for replying????? good idea but I want to broadcasts sound in different areas
SAKryukov at 22-Jun-11 18:33pm
   
I assumed this possibility in my answer (last paragraph). So, are you saying detailed (or any) synchronization is not critical, and all sources of audio are not listened by the same person? Then it's a different story.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 15 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100