Playing sounds simultaneously

I know how to play an audio file using the SoundPlayer class, but I have only been able to play one sound at a time. If I am playing a sound, and I try to play another sound, it stops the first one to play the second one. And that is with two separately declared SoundPlayer objects. So, is there some way to play two sounds simultaneously I want to be able to play background music and sound effects at the same time.

Answer this question

Playing sounds simultaneously

  • robhare

    ah ok I see!

    I don't think you can do this using the SoundPlayer classes (do not quote me) but perhaps using Managed DirectX. (SDK)

    There also includes various samples and documentation of course on how to use the Managed DirectX classes etc...

    http://msdn.microsoft.com/directx/sdk/

    More specifically in there, take a look at the AudioVideoPlayerback namespaces

    infact I just did to confirm. Once installed, add a reference to Microsoft.DirectX.AudioVideoPlayback then import the namespace (using Microsoft.DirectX.AudioVideoPlayback) then....

    Audio theFirstFile = new Audio("fileName1.mp3");

    Audio theSecondFile = new Audio("fileName2.mp3");

    theFirstFile.Play();

    theSecondFile.Play();

    and both of the files will play at the same time without your application or rather the main thread (UI) from hanging. Does this help



  • GraemeH

    Thank you, but I am already playing the sound Async. The problem isn't that it waits until the sound is over. The problem is that when I'm playing a sound Async, and I play another sound before the first one is over, it stops the first one so that the second sound can play. It's like it can only play one at a time. I didn't know if there is a way around this or not. I would like to have my background music PlayLooping, and an effect Playing while the first is looping.
  • WizMan

    Thank you, that worked great! I'll just have to learn more about how things work so I can loop, etc. I will check out the website you listed. Thank you very much. I was scared to try to figure out playing a sound with directx, but it seems to be just as easy as SoundPlayer.
  • PublicError

    how are you playing the file

    you can play the sound in Async mode meaning it will carry on the executing of your program whilst doing its play sound in the background....by default it will not do this but wait until the sound has finished playing, then carry on. To do this:

    System.Media.SoundPlayer thePlayer1 = new System.Media.SoundPlayer("FileName.wav");

    thePlayer1.Play();

    System.Media.SoundPlayer thePlayer2 = new System.Media.SoundPlayer("FileName2.wav");

    thePlayer2.Play();

     

    You also have an option of playing the sound in a loop if you like

    thePlayer.PlayLooping();

    and of course stop playing by calling the Stop method:

    thePlayer.Stop();

     

    does this help/give you some pointers



  • Playing sounds simultaneously