2013-01-23 158 views
4

我試圖播放位於我的項目文件夾中的.Wav文件。用C播放.wav文件#

聲音文件位於「資源/聲音/ slot_roll_on.Wav」

資源文件夾是我自己創建的,在項目的根文件夾。

這是我使用運行.wav文件

 Assembly a = Assembly.GetExecutingAssembly(); 
     Stream s = a.GetManifestResourceStream("kisscam.g.resources.Resources.Sounds.slot_roll_on.wav"); 
     SoundPlayer snd = new SoundPlayer(s); 
     snd.Play(); 

我無法讓播放聲音,我不斷收到Windows聲音不發現聲音的代碼。

某處堆棧溢出我發現這個代碼找到正確的組裝路徑應該是什麼。

  Assembly a = Assembly.GetExecutingAssembly(); 
      string[] resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames(); 
      int i; 
      for (i = 0; i < resourceNames.Length; i++) 
      { 

       MessageBox.Show(resourceNames[i]); 


      } 

它輸出這兩個路徑

kisscam.Properties.Resources.resources 

kissscam.g.resources 

我試着用他們兩個,但他們沒有工作。

任何人都知道我在做什麼錯了?

+1

試圖修改你的問題:) – spajce

+0

是的,對不起,我的英語不是很好。你不明白什麼? – Arcade

+1

使用ildasm.exe並查看.mresource名稱的程序集清單。 –

回答

7

通過轉到項目屬性 - >資源選擇音頻並瀏覽到該文件,將您的Wav文件添加到資源。您將能夠看到它作爲Propeties.Resources的一部分。將它添加到一個資源文件夾,你可以將其設置爲嵌入或離開它是,它被設置爲內容

enter image description here

訪問了如下

private void button1_Click(object sender, EventArgs e) 
{ 
    SoundPlayer snd = new SoundPlayer(Properties.Resources.tada); 
    snd.Play(); 

} 
4

如果你想添加通過在項目中播放您的.wav文件,在您的程序中播放音樂。然後你必須像這樣添加.wav文件。

using System.Media; // write down it at the top of the FORM 

    SoundPlayer my_wave_file = new SoundPlayer("F:/SOund wave file/airplanefly.wav"); 
    my_wave_file.PlaySync(); // PlaySync means that once sound start then no other activity if form will occur untill sound goes to finish 

請記住,你寫的文件的路徑與正斜槓(/)格式,不要使用反斜槓()期間給予的文件的路徑,否則你將得到一個錯誤