wpfsoundplayer

WPF SoundPlayer multi-channel sound?


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?


Solution

  • 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.