如何从资源播放音频

问题描述:

我想从使用.NET Compact Framework的资源播放音频。我增加了我的应用程序资源属性的音频文件,并正在尝试使用下面的样本资源文件引用代码...如何从资源播放音频

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

但这代码没有发挥出WAV声音。如何使用.NET Compact Framework 3.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

我得到了解决方案。此代码是在.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

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

这应该为你工作:

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

请确保您有using System.Media命名空间的上方。