2010-11-08 156 views
30

如何從我的項目資源中播放WAV音頻文件?我的項目是C#中的Windows Forms應用程序。如何從資源播放WAV音頻文件?

+0

[這是你正在尋找。](http://msdn.microsoft.com/en-us/library/3w5b27z4.aspx) – 2010-11-08 16:23:39

回答

35

因爲mySoundFileStream,你可以採取的SoundPlayer的重載的構造函數,它接受一個Stream對象的優勢:

System.IO.Stream str = Properties.Resources.mySoundFile; 
System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str); 
snd.Play(); 

SoundPlayer Class Documentation (MSDN)

+1

這將在Windows CE中引發異常,因爲它不會自動將資源從byte []轉換爲流。我發現在這種情況下有以下答案。將它留在這裏爲其他人: http://stackoverflow.com/questions/1900707/how-to-play-audio-from-resource – Hagelt18 2014-12-31 18:21:19

+0

我們實際上不需要申報一個單獨的流變量;) – TomeeNS 2016-06-10 00:00:16

+2

@TomeeNS:當然,但它向人們顯示資源的類型和所使用的'SoundPlayer'構造函數的重載。 – 2016-06-13 13:51:26

2

你需要謹慎對待垃圾收集器釋放存儲器在聲音仍在播放時使用的內存。雖然它很少發生,但當它發生時,你只會播放一些隨機存儲器。有一個解決方案,完成與源代碼實現你想在這裏:http://msdn.microsoft.com/en-us/library/dd743680(VS.85).aspx

滾動到最底部,在「社區內容」部分。

0

當您必須將聲音添加到您的項目中時,您將通過播放.wav文件來完成此操作。然後你必須添加這樣的.wav文件。

using System.Media; //write this at the top of the code 

    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 

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

另請注意,如果您希望在播放聲音時發生其他事情,則可以使用my_wave_file.PlayAsync();更改my_wave_file.PlaySync();

30

a)OK,首先將音頻文件(.wav)添加到項目資源中。

  1. 從菜單工具欄(「VIEW」)打開「解決方案資源管理器」或者只需按Ctrl + Alt + L。
  2. 點擊「屬性」的下拉列表。
  3. 然後選擇「Resource.resx」並按回車。

open project resource

  • 現在從組合框列表中選擇 「音頻」。
  • add audio files to resource

  • 然後點擊 「添加資源」,選擇的音頻文件(.WAV)和點擊 「打開」。
  • browsing for audio files

  • 選擇的音頻文件(一個或多個)和改變爲 「中的.resx嵌入式」, 「持續性」 的性質。
  • embedding audio files to resource

    b)現在,只寫這段代碼播放音頻。

    在這段代碼中,我在表單加載事件上播放音頻。

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    
    using System.Media; // at first you've to import this package to access SoundPlayer 
    
    namespace WindowsFormsApplication1 
    { 
        public partial class login : Form 
        { 
         public login() 
         { 
          InitializeComponent(); 
         } 
    
         private void login_Load(object sender, EventArgs e) 
         { 
          playaudio(); // calling the function 
         } 
    
         private void playaudio() // defining the function 
         { 
          SoundPlayer audio = new SoundPlayer(WindowsFormsApplication1.Properties.Resources.Connect); // here WindowsFormsApplication1 is the namespace and Connect is the audio file name 
          audio.Play(); 
         } 
        } 
    } 
    

    就是這樣。
    全部完成,現在運行該項目(按f5)並享受您的聲音。
    一切順利。 :)

    +1

    正是我在找的東西!照片幫了很多。你知道我是否可以用這種方式嵌入字體(如fontawesome)以及在我的程序中使用? – 2016-08-02 19:52:15

    +0

    當我添加一個wav文件時,它添加了一個Resources2.Designer.cs,它似乎是Resources.Designer.cs的一個重複項 - 從而給我帶來衝突。這剛剛開始發生。任何想法可能發生什麼? https://www.screencast.com/t/xAVpE5v6b0 – Matt 2017-12-07 22:05:21