wpf 如何用WPF同时播放两个声音文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13925840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play two sound file at the same time with WPF?
提问by Alan lau
I use SoundPlayerto play sound effects in the WPF program. However, I find that when two sounds effects are played at the same time, the new one will replace the old one (i.e. the new will terminate the old and play itself), but what I want is to keep playing the old one even when the new one is played.
我用来SoundPlayer在WPF程序中播放音效。但是,我发现当两个音效同时播放时,新的会取代旧的(即新的会终止旧的并自行播放),但我想要的是即使在播放时继续播放旧的新的播放。
SoundPlayer wowSound = new SoundPlayer("soundEffect/Wow.wav");
SoundPlayer countingSound = new SoundPlayer("soundEffect/funny.wav");
wowSound.Play(); // play like background music
countingSound.Play(); // from click to generate the sound effect
采纳答案by Picrofo Software
You may use SoundPlayer.PlaySync()which plays the .wavfile using the User Interface thread so that wowSoundwould be played first. Then, countingSoundwill be played after wowSoundhas finished playing
您可以使用SoundPlayer.PlaySync()which.wav使用用户界面线程播放文件,以便wowSound首先播放。然后,countingSound将在播放完毕wowSound后播放
Example
例子
SoundPlayer wowSound = new SoundPlayer(@"soundEffect/Wow.wav"); //Initialize a new SoundPlayer of name wowSound
SoundPlayer countingSound = new SoundPlayer(@"soundEffect/funny.wav"); //Initialize a new SoundPlayer of name wowSound
wowSound.PlaySync(); //Play soundEffect/Wow.wav synchronously
countingSound.PlaySync(); //Play soundEffect/funny.wav synchronously
NOTICE: You can not play more than ONE sound at the same time using SoundPlayeras it does not support playing simultaneous sounds. If you would like to play TWO or more sounds at once, System.Windows.Media.MediaPlayerwould be a better option
注意:您不能同时播放一种以上的声音,SoundPlayer因为它不支持同时播放声音。如果您想一次播放两个或更多声音,System.Windows.Media.MediaPlayer将是更好的选择
Example
例子
MediaPlayer wowSound = new MediaPlayer(); //Initialize a new instance of MediaPlayer of name wowSound
wowSound.Open(new Uri(@"soundEffect/Wow.wav")); //Open the file for a media playback
wowSound.Play(); //Play the media
MediaPlayer countingSound = new MediaPlayer(); //Initialize a new instance of MediaPlayer of name countingSound
countingSound.Open(new Uri(@"soundEffect/funny.wav")); //Open the file for a media playback
countingSound.Play(); //Play the media
回答by Dean Kuga
using System.Threading.Tasks;
private void MyMethod()
{
Task.Factory.StartNew(PlaySound1);
Task.Factory.StartNew(PlaySound2);
}
private void PlaySound1()
{
SoundPlayer wowSound = new SoundPlayer("soundEffect/Wow.wav");
wowSound.Play();
}
private void PlaySound2()
{
SoundPlayer countingSound = new SoundPlayer("soundEffect/funny.wav");
countingSound.Play();
}
EDIT:
编辑:
Picrofo is right, it can't be done this way but it appears you can achieve this using DirectShow .NETwhich is just a wrapper around the MS DirectShow...
Picrofo 是对的,它不能以这种方式完成,但看来您可以使用DirectShow .NET来实现这一点,它只是 MS DirectShow 的一个包装器...
回答by AkselK
After using the built in players in WPF and C# for a while, I found them to be too lacking in features so I moved to NAudio instead.
在 WPF 和 C# 中使用内置播放器一段时间后,我发现它们太缺乏功能,所以我转而使用 NAudio。
It requires you to write a little more code, but you'll be glad once you discover that the requirements have changed (which they always do) and the built in Media Players won't do anymore.
它需要您编写更多的代码,但是一旦您发现需求已经改变(他们总是这样做)并且内置的媒体播放器将不再这样做,您就会很高兴。
The source code comes with a few samples and you can find more help both here on Stack Overflow, on the codeplex site and just by searching Google.
源代码附带了一些示例,您可以在 Stack Overflow、codeplex 站点和 Google 搜索中找到更多帮助。

