Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play two sounds simultaneusly

Tags:

c#

winforms

Is there a way to play two sounds at the same time?

I know that SoundPlayer isn't able to do this. I can't use SoundEffect as I believe it's only part of XNA.

The two required sounds will be called at unknown and random times. The sound needs to be be controlled after it is played. i.e., the sound must be able to be stopped before it has finished playing.

like image 834
AlanFoster Avatar asked Jun 04 '11 23:06

AlanFoster


People also ask

How can I play multiple WAV files at once?

You can use DirectShow transform filter it is a Microsoft Windows Application Programming Interface (API) that enables Windows applications to interact with and control Windows "Media" input devices, Or you can use WaveMix DLL it is a utility that allows multiple WAV files to be played simultaneously.

Can you overlap sounds on Soundpad?

That's not possible with Soundpad. More instances won't change it as in the backend it is down to one sound at a time. You need virtual audio drivers to do this or prepare the sounds beforehand with additional editing software.

How many sounds can Unity play at once?

For example, if the real voice limit is 32 (the default) and if there are 32 playing Audio Sources in the scene, but only one is actually audible, Unity will play all 32 sounds regardless.


2 Answers

Reference PresentationCore and WindowsBase and try this...

var p1 = new System.Windows.Media.MediaPlayer(); p1.Open(new System.Uri(@"C:\windows\media\tada.wav")); p1.Play();  // this sleep is here just so you can distinguish the two sounds playing simultaneously System.Threading.Thread.Sleep(500);  var p2 = new System.Windows.Media.MediaPlayer(); p2.Open(new System.Uri(@"C:\windows\media\tada.wav")); p2.Play(); 

EDIT I received a downvote probably because at first glance this looks like it will play the second sound after the first is finished. It doesn't, they are played by windows asynchronously. The sleep is there so if you test this code verbatim you can hear the sounds play together, it wouldn't be noticeable without the delay since they are the same sound.

This code demonstrates the two sounds playing on separate threads on top of each other, which is sort of pointless since the playback doesn't block anyway

new System.Threading.Thread(() => {         var c = new System.Windows.Media.MediaPlayer();         c.Open(new System.Uri(@"C:\windows\media\tada.wav"));         c.Play();     }).Start();  System.Threading.Thread.Sleep(500);  new System.Threading.Thread(() => {         var c = new System.Windows.Media.MediaPlayer();         c.Open(new System.Uri(@"C:\windows\media\tada.wav"));         c.Play();     }).Start(); 

http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.stop.aspx The class also has the control you need to stop playback

like image 95
JeremyWeir Avatar answered Sep 29 '22 06:09

JeremyWeir


The "MediaPlayer" object will not let you play two sounds at once, even if you create two instances. You will need to bring in the native windows API "mciSendString".

    [DllImport("winmm.dll")]     static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);      public Form1()     {         InitializeComponent();          mciSendString(@"open C:\Users\Jono\Desktop\applause.wav type waveaudio alias applause", null, 0, IntPtr.Zero);         mciSendString(@"play applause", null, 0, IntPtr.Zero);          mciSendString(@"open C:\Users\Jono\Desktop\foghorn.wav type waveaudio alias foghorn", null, 0, IntPtr.Zero);         mciSendString(@"play foghorn", null, 0, IntPtr.Zero);      } 
like image 23
feathj Avatar answered Sep 29 '22 08:09

feathj