4

I have an app that emits sounds at a random interval. When two sounds collide they do not nicely overplay each other but one interrupts the other.

I'm using SoundPlayer to play the sounds. (this is a WPF app)

How can I use multiple sound channels so that if two sounds occur at the same time they are both played at the same time instead of one interrupting the other?

Kelly
  • 6,992
  • 12
  • 59
  • 76
  • usually multi-channel refers to separate speakers. do you look forward for the same? or you are looking for multiple stream of sounds playing simultaneously? – pushpraj Aug 20 '14 at 03:53
  • I'm looking for two sounds playing at the same time. I have multiple sounds that occur almost at the same time and would like them to overlap. Right now the second sound cuts the first one short. – Kelly Sep 19 '14 at 03:58
  • try playing the different sounds using different players. – pushpraj Sep 19 '14 at 04:03
  • Still doesn't seem to work. Task.Run(() => { var player = new SoundPlayer(@"c:\Computer_Magic.wav"); player.Play(); }); Put that on a button and click it and you'll see it will interrupt the first call. – Kelly Sep 19 '14 at 04:16
  • as an alternative try using `MediaElement` also, may it solve your issue. – pushpraj Sep 19 '14 at 04:22
  • 1
    Yeah I tried it but the same issue. It seems the MediaElement and Soundplayer both use a single channel. What is required is multi-channel sound such as in XNA. Unfortunately XNA is discontinued, so I suppose the only real choice is DirectX currently. – Kelly Sep 30 '14 at 02:37

1 Answers1

4

To my knowledge SoundPlayer uses the native PlaySound function which does not support playing multiple sound simultaneously.

You can however do this with System.Windows.Media.MediaPlayer.

var p1 = new MediaPlayer();
p1.Open(new Uri(@"C:\windows\media\ringout.wav"));
p1.Play();

System.Threading.Thread.Sleep(250);

var p2 = new MediaPlayer();
p2.Open(new System.Uri(@"C:\windows\media\ringout.wav"));
p2.Play();

The sleep is there simply to add a bit of a delay to the playback, making it clear that the two sounds do actually play at the same time.

Rune Vikestad
  • 4,642
  • 1
  • 25
  • 36
  • How does the delay show that they are playing at the same time? – Trevor Tubbs Feb 02 '16 at 02:40
  • Could have swore I tried that, but thank you so much, enjoy the bounty! – Kelly Feb 02 '16 at 12:42
  • @Trevor It'll will start playing the second sound 250 milliseconds later than the first one, making it easier for us humans to hear that the two sounds do actually overlap. You could probably hear it without the sleep, but it wouldn't be as obvious. – Rune Vikestad Feb 03 '16 at 07:42