有没有办法同时播放两个声音?我知道SoundPlayer
不能做到这一点。 我不能使用SoundEffect
,因为我相信它只是XNA的一部分。同时播放两个声音
这两个需要的声音将在未知和随机时间被调用。播放后需要控制声音。即声音必须能够在其完成播放之前停止。
有没有办法同时播放两个声音?我知道SoundPlayer
不能做到这一点。 我不能使用SoundEffect
,因为我相信它只是XNA的一部分。同时播放两个声音
这两个需要的声音将在未知和随机时间被调用。播放后需要控制声音。即声音必须能够在其完成播放之前停止。
参考PresentationCore
和WindowsBase
和尝试这个...
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();
编辑 我,因为乍一看,这看起来像第一完成后,将播放第二个声音接到downvote可能。它不会,它们是通过异步的窗口播放的。睡眠在那里,所以如果你逐字测试这段代码,你可以听到声音一起播放,如果没有延迟,它们就不会被察觉,因为它们是相同的声音。
此代码演示了两个声音播放在单独的线程上对方,这是那种毫无意义的,因为上面的播放不会阻止反正
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 类也有您需要的控制停止播放
即使创建了两个实例,“MediaPlayer”对象也不会让您一次播放两个声音。您将需要引入本机窗口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);
}
检查PlaySound
方法这里http://msdn.microsoft.com/en-us/library/aa909766.aspx,其标志SND_ASYNC
。
解决方案: 嗨, 我正在开发一个WP8应用程序,我需要多个声音同时播放,上面提到的解决方案并不适合我,所以我使用了XNA框架。这里是链接
http://msdn.microsoft.com/en-us/library/ff842408.aspx
,然后播放乌尔声音文件是这样的...
SoundEffect Sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri("Assets/Sounds/wav/sound.wav", UriKind.Relative)).Stream);
Sound.Play();
对于循环...
SoundEffectInstance Sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri("Assets/Sounds/wav/sound.wav", UriKind.Relative)).Stream).CreateInstance();
Sound.IsLooped = true;
Sound.Play();
注:文件必须为“.wav”(PCM,8或16位,8KHz到48KHz,单声道或立体声)格式
从http://alvas.net/alvas.audio,samples.aspx#sample7个http://alvas.net/alvas.audio,samples.aspx#sample6
Player pl = new Player();
byte[] arr = File.ReadAllBytes(@"in.wav");
pl.Play(arr);
Player pl2 = new Player();
pl2.FileName = "123.mp3";
pl2.Play();
或混合音频数据之前打How to mix to mix two audio file..
private void Mix(string outfile, string infile1, string infile2, int shiftSec)
{
WaveReader wr1 = new WaveReader(File.OpenRead(infile1));
WaveReader wr2 = new WaveReader(File.OpenRead(infile2));
IntPtr format1 = wr1.ReadFormat();
WaveFormat wf = AudioCompressionManager.GetWaveFormat(format1);
WaveWriter ww = new WaveWriter(File.Create(outfile), AudioCompressionManager.FormatBytes(format1));
byte[] data0 = wr1.ReadData(0, shiftSec);
byte[] data1 = wr1.ReadData(shiftSec);
byte[] data2 = wr2.ReadData();
byte[] mixData = AudioCompressionManager.Mix(format1, data2, data1);
ww.WriteData(data0);
ww.WriteData(mixData);
ww.Close();
wr2.Close();
wr1.Close();
}
什么环境? ASP.NET/web?的WinForms? WPF桌面? Silverlight的? – 2011-06-04 23:29:46
嗨,这是一个Windows窗体应用程序! – AlanFoster 2011-06-04 23:34:14