2009-12-14 85 views
5

我想從使用.NET Compact Framework的資源播放音頻。我增加了我的應用程序資源屬性的音頻文件,並正在嘗試使用下面的樣本資源文件引用代碼...如何從資源播放音頻

SoundPlayer player = new SoundPlayer(Assembly.GetExecutingAssembly(). 
    GetManifestResourceStream("blessedwav.wav")); 
player.Play(); 

但這代碼沒有發揮出WAV聲音。如何使用.NET Compact Framework 3.5播放資源音頻文件?

回答

5

試試這個:

//I added the file as a audio resource in my project 
SoundPlayer player = new SoundPlayer(Properties.Resources.recycle); 
player.Play(); 

我沒有使用.NET Compact Framework的嘗試。但它在C#中爲我工作。

+0

上午嘗試此代碼,但它不工作.Net CF, 它顯示錯誤:System.Media.SoundPlayer.SoundPlayer(字符串)的最佳重載方法匹配'有一些無效的參數。 \t 反正謝謝anuraj – user228502 2009-12-15 09:49:21

+0

嗯,所以我認爲.Net CF只有一個使用字符串作爲參數的重載。那麼爲什麼你不把文件保存到臨時位置並從那裏播放。 – Anuraj 2009-12-16 07:42:40

6

我得到了解決方案。此代碼是在.NET Compact Framework中工作得很好:

// Convert a byte array to a stream 
using (var audioStream = new MemoryStream(Properties.Resources.full_song_wav)) 
{ 
    using (var player = new SoundPlayer(audioStream)) 
    { 
     player.Play() 
    } 
} 
+0

很高興聽到你有解決方案:) – Anuraj 2009-12-16 07:43:21

0
Resources.ResourceManager.GetStream("nudgeSound", Resources.Culture); 
System.IO.Stream s = Resources.ResourceManager.GetStream("nudgeSound", Resources.Culture); 
SoundPlayer player = new SoundPlayer(s); 
player.Play(); 
0

這應該爲你工作:

Stream str = Properties.Resources.YourWaveSoundFile; 
SoundPlayer plyr = new SoundPlayer(str); 
plyr.Play(); 

請確保您有using System.Media命名空間的上方。