0

My question is similar to one here : Play audio to two different Audiodevices simultaneously with Naudio

But i ask it here again since it has not been answered clearly in the link above.

I also have it at: Play sound in both speaker and headset wpf

Inspiration from : Play a sound in a specific device with C#

I am adding source code and adding the NAudio tag here as well.

I have an wpf application and i am using the soundPlayer class to play sound (for eg ringtone). Currently the tone plays either on speakers or on the headset (if its plugged in). I would like the application to play the tone on speaker even when the headsets are plugged in. I know there are ways to do this in android, but couldn't find any in wpf. I would also like an UI for the user to choose the devices in which he would like to hear sound. Any help is appreciated. Thanks !

public void detectDevices()
{
    int waveOutDevices = WaveOut.DeviceCount;
    switch (waveOutDevices)
    {
        case 1:
            var wave1 = new WaveOut();
            wave1.DeviceNumber = 0;
            playSound(0); 

            break;
        case 2:
            var wave2 = new WaveOut();
            wave2.DeviceNumber = 0;
            playSound(0);

            var wave3 = new WaveOut();
            wave3.DeviceNumber = 1;
            playSound(1); 

            break;

    }
}

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;
    output = waveOut;
    output.Init(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;
    }
}

case eSelector.startIncomingRinging:

            fileName = ("Ring.wav");
            detectDevices();

I still hear ringtone just in one device (either in headset or speakers) using the code above.

Community
  • 1
  • 1
user2054702
  • 69
  • 1
  • 11

1 Answers1

1

You need two instances of WaveOut, one for each soundcard. And then the simplest way if you are playing from file, is to also have two instances of WaveFileReader. You can't easily synchronize them I'm afraid, you'll just have to start them both playing together and hope for the best.

The wave1, wave2 and wave3 classes in your code above do abosolutely nothing. The audio will be played with the WaveOut device you create in playSound. You seem to have a single class property called output and another called waveReader, when you need two of each.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Thanks ! I have it playing on both devices and the sound is not in perfect sync (which is fine by me), but the sound is disturbed in one of the devices. On the other device it plays just fine. Any suggestions to improve on this ? – user2054702 Mar 31 '14 at 14:09
  • depends what you mean by disturbed. Can you reproduce it when playing only to one device? – Mark Heath Mar 31 '14 at 14:53
  • By disturbed i mean the sound seems to be sliced up and distorted always on one device, while on the other device it plays just fine. I have tried USB headsets from different brands, while my PC speakers are Realtek. It is not constant on which device the sound is distorted. – user2054702 Apr 01 '14 at 10:40
  • Another quick question. Will this solution (playing sound on two devices) work on windows xp as well ? Cause the API s have changed since windows XP and hence my question. Thanks ! – user2054702 Apr 01 '14 at 15:31