I'm looking for a way to play multiples sound back to back asynchronously.
I actually have :
jouerSon("_" + nbre1);
jouerSon(operateur);
jouerSon("_" + nbre2);
jouerSon("equal");
public void jouerSon(String son)
{
System.Media.SoundPlayer player = new SoundPlayer();
player.Stream = Properties.Resources.ResourceManager.GetStream(son);
// player.LoadAsync();
player.Play();
//Thread.Sleep(500);
//player.Stop();
}
I'd like to play the first sound, then when it's over the second one etc all while the program is still responsive.
I managed to play each sound one after another but only using a synchronous play, which prevent the user from doing anything until all sound are played.
And if I try using with an asynchronous play, only the last sound is played instead of each one.
I've looked around for a solution, but can't seem to find any. Could someone help me?
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();
}