是否可以使用 SoundPlayer 连续播放 2 个声音? [英] Is it possible to play 2 sounds back to back using SoundPlayer?
问题描述
我正在寻找一种异步播放多重声音的方法.
I'm looking for a way to play multiples sound back to back asynchronously.
其实我有:
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?
推荐答案
在不同的线程上播放:
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();
}
这篇关于是否可以使用 SoundPlayer 连续播放 2 个声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!