9

I use SoundPlayer to play sound effects in the WPF program. However, I find that when two sounds effects are played at the same time, the new one will replace the old one (i.e. the new will terminate the old and play itself), but what I want is to keep playing the old one even when the new one is played.

SoundPlayer wowSound = new SoundPlayer("soundEffect/Wow.wav");

SoundPlayer countingSound = new SoundPlayer("soundEffect/funny.wav");

wowSound.Play(); // play like background music

countingSound.Play();  // from click to generate the sound effect
Alan lau
  • 111
  • 2
  • 10

3 Answers3

7

You may use SoundPlayer.PlaySync() which plays the .wav file using the User Interface thread so that wowSound would be played first. Then, countingSound will be played after wowSound has finished playing

Example

SoundPlayer wowSound = new SoundPlayer(@"soundEffect/Wow.wav"); //Initialize a new SoundPlayer of name wowSound
SoundPlayer countingSound = new SoundPlayer(@"soundEffect/funny.wav"); //Initialize a new SoundPlayer of name wowSound
wowSound.PlaySync(); //Play soundEffect/Wow.wav synchronously
countingSound.PlaySync();  //Play soundEffect/funny.wav synchronously 

NOTICE: You can not play more than ONE sound at the same time using SoundPlayer as it does not support playing simultaneous sounds. If you would like to play TWO or more sounds at once, System.Windows.Media.MediaPlayer would be a better option

Example

MediaPlayer wowSound = new MediaPlayer(); //Initialize a new instance of MediaPlayer of name wowSound
wowSound.Open(new Uri(@"soundEffect/Wow.wav")); //Open the file for a media playback
wowSound.Play(); //Play the media

MediaPlayer countingSound = new MediaPlayer(); //Initialize a new instance of MediaPlayer of name countingSound
countingSound.Open(new Uri(@"soundEffect/funny.wav")); //Open the file for a media playback
countingSound.Play(); //Play the media
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • 1
    I have tried PlaySync() method, but I find that the new is just playing after the old one. I wanna play both at the same time. You may think the wowSound.Play() is playing like background music and countingSound.Play() is played by click. – Alan lau Dec 18 '12 at 03:47
  • @Alanlau If you would like to play the both sounds at the same time, I think that you may try `Play()`. Have a nice day :) – Picrofo Software Dec 18 '12 at 03:48
  • Picrofo, I hope a nice too, but I find that the Play() method will just let the new replace the old. In other words, the new will terminate the old and play itself. – Alan lau Dec 18 '12 at 03:51
  • @Alanlau Sorry for misunderstanding, I've updated my answer. I hope you get your issue resolved as soon as possible. Have a nice day :) – Picrofo Software Dec 18 '12 at 04:09
  • @Alanlau I'm glad I could help. Happy Holidays! :) – Picrofo Software Dec 27 '12 at 09:12
  • The code for MediaPlayer does not bomb but it also does not work. The only thing that seems to make a sound in wpf is SoundPlayer but it is very limited and clunky. – Mr Nellinger Jun 24 '22 at 18:33
0
using System.Threading.Tasks;

private void MyMethod()
{
    Task.Factory.StartNew(PlaySound1);
    Task.Factory.StartNew(PlaySound2);
}

private void PlaySound1()
{
    SoundPlayer wowSound = new SoundPlayer("soundEffect/Wow.wav");
    wowSound.Play();
}

private void PlaySound2()
{
    SoundPlayer countingSound = new SoundPlayer("soundEffect/funny.wav");
    countingSound.Play();
}

EDIT:

Picrofo is right, it can't be done this way but it appears you can achieve this using DirectShow .NET which is just a wrapper around the MS DirectShow...

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • 1
    I do not really think that this will work even if you try to run a new `Thread` to play the wave files. `SoundPlayer` uses the Native WINAPI function to play the wave files which unfortunately does not support playing simultaneous sounds. Have a nice day :) – Picrofo Software Dec 18 '12 at 13:17
0

After using the built in players in WPF and C# for a while, I found them to be too lacking in features so I moved to NAudio instead.

http://naudio.codeplex.com/

It requires you to write a little more code, but you'll be glad once you discover that the requirements have changed (which they always do) and the built in Media Players won't do anymore.

The source code comes with a few samples and you can find more help both here on Stack Overflow, on the codeplex site and just by searching Google.

AkselK
  • 2,563
  • 21
  • 39