Play multiple sounds using SoundPlayer
You'll need to use DirectX (DirectSound) or some similar API that is designed to allow the playing of multiple sounds at the same time.
Playing multiple wav files with SoundPlayer() without the sounds overlapping
If you can afford stopping your main thread, you can use
SoundPlayer.PlaySync()
that loads and plays the .wave file on the calling thread.
.NET SoundPlayer.PlaySync() documentation
Play two sounds simultaneusly
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
Is it possible to play 2 sounds back to back using SoundPlayer?
Play them on a different thread:
private void button1_Click(object sender, EventArgs e)
{
// ...
jouerSon(new String[] { "_" + nbre1, operateur, "_" + nbre2, "equal" });
// ...
}
public void jouerSon(String[] sons)
{
Task t = new Task(() =>
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
foreach (String son in sons)
{
player.Stream = Properties.Resources.ResourceManager.GetStream(son);
player.PlaySync();
}
});
t.Start();
}
Or possibly something more specifically tailored, like this:
private void button1_Click(object sender, EventArgs e)
{
// ...
jouerSon(nbre1, operateur, nbre2);
// ...
}
public void jouerSon(string nbre1, string operateur, string nbre2)
{
Task t = new Task(() =>
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.Stream = Properties.Resources.ResourceManager.GetStream("_" + nbre1);
player.PlaySync();
player.Stream = Properties.Resources.ResourceManager.GetStream(operateur);
player.PlaySync();
player.Stream = Properties.Resources.ResourceManager.GetStream("_" + nbre2);
player.PlaySync();
player.Stream = Properties.Resources.ResourceManager.GetStream("equal");
player.PlaySync();
});
t.Start();
}
Related Topics
How to Suppress a Stylecop Warning
Which Blocking Operations Cause an Sta Thread to Pump Com Messages
How Does One Extract Each Folder Name from a Path
What Is the Meaning of Thread-Agility in ASP.NET
Install to Same Path When Upgrading Application
How to Implement a Rule Engine
Httpclient and Using Proxy - Constantly Getting 407
How to Ignore a Property When Serializing Using the Datacontractserializer
C# Instantiate Generic List from Reflected Type
Using Tfs API, How to Find the Comments Which Were Made on a Code Review
Func Delegate with Ref Variable
Changing Font for Richtextbox Without Losing Formatting
Uploading Files Not Working - Need Assistance
Good Introduction to the .Net Reactive Framework