Click here to Skip to main content
15,888,042 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
<br />
    public partial class PlaybackForm : Form<br />
    {<br />
    IWavePlayer waveOut;<br />
    string fileName = null;<br />
    WaveStream mainOutputStream;<br />
    WaveChannel32 volumeStream;<br />
    int _deviceNum;<br />
    int _deviceNum1;<br />
    Thread t1;<br />
    Thread t2;<br />
    public PlaybackForm(int deviceNum,int deviceNum1)<br />
    {<br />
        InitializeComponent();<br />
        _deviceNum = deviceNum;<br />
        _deviceNum1 = deviceNum1;<br />
    }<br />
    private void buttonPlay_Click(object sender, EventArgs e)<br />
    {<br />
        if (waveOut != null)<br />
        {<br />
            if (waveOut.PlaybackState == PlaybackState.Playing)<br />
            {<br />
              return;<br />
            }<br />
            else if (waveOut.PlaybackState == PlaybackState.Paused)<br />
            {<br />
                waveOut.Play();<br />
                return;<br />
            }<br />
        }<br />
        // we are in a stopped state<br />
        // TODO: only re-initialise if necessary<br />
        if (String.IsNullOrEmpty(fileName))<br />
        {<br />
            toolStripButtonOpenFile_Click(sender, e);<br />
        }<br />
        if (String.IsNullOrEmpty(fileName))<br />
        {<br />
            return;<br />
        }<br />
        try<br />
        {<br />
            CreateWaveOut();<br />
        }<br />
        catch (Exception driverCreateException)<br />
        {<br />
            MessageBox.Show(String.Format("{0}", driverCreateException.Message));<br />
            return;<br />
        }<br />
        mainOutputStream = CreateInputStream(fileName);<br />
        trackBarPosition.Maximum = (int)mainOutputStream.TotalTime.TotalSeconds;<br />
        labelTotalTime.Text = String.Format("{0:00}:{1:00}", (int)mainOutputStream.TotalTime.TotalMinutes,<br />
            mainOutputStream.TotalTime.Seconds);<br />
        trackBarPosition.TickFrequency = trackBarPosition.Maximum / 30;<br />
        try<br />
        {<br />
            waveOut.Init(mainOutputStream);<br />
        }<br />
        catch (Exception initException)<br />
        {<br />
            MessageBox.Show(String.Format("{0}", initException.Message), "Error Initializing Output");<br />
            return;<br />
        }<br />
        // not doing Volume on IWavePlayer any more<br />
        volumeStream.Volume = volumeSlider1.Volume;<br />
        waveOut.Play();<br />
    }<br />

And this is how to create the waveout
<br />
  private void CreateWaveOut()<br />
    {<br />
        CloseWaveOut();<br />
        int latency = (int)comboBoxLatency.SelectedItem;<br />
        //if (radioButtonWaveOut.Checked)<br />
        {<br />
            //WaveCallbackInfo callbackInfo = checkBoxWaveOutWindow.Checked ?<br />
            WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();<br />
            // WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();<br />
            // WaveCallbackInfo.NewWindow(): WaveCallbackInfo.FunctionCallback();<br />
            WaveOut outputDevice = new WaveOut(callbackInfo);<br />
            outputDevice.DesiredLatency = latency;<br />
            outputDevice.DeviceNumber = _deviceNum;<br />
            waveOut = outputDevice;<br />
        }<br />
    }<br />

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
Comments
Sergey Alexandrovich Kryukov 15-Jun-11 15:38pm    
Interesting question, my 5, but I would prefer to know the goal of all this activity :-)
--SA
Nicole castel 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

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
 
Share this answer
 
Comments
Nicole castel 22-Jun-11 15:50pm    
than kou for replying????? good idea but I want to broadcasts sound in different areas
Sergey Alexandrovich Kryukov 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900