有沒有辦法同時播放兩個聲音?我知道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