用C#播放.wav文件
我正在尝试播放位于项目内部文件夹中的.Wav文件。
声音文件位于“Resource / Sounds / 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();
我无法播放声音,我不停地发出声音,因为找不到声音。
在堆栈溢出的某处,我发现这段代码可以找到正确的汇编路径。
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
我尝试过使用它们,但它们都不起作用。
谁知道我做错了什么?
通过转到项目属性 – >资源选择音频并浏览到该文件,将您的Wav文件添加到资源。 然后,您将能够将其视为Propeties.Resources
一部分。 它会将它添加到资源文件夹,您可以在其中将其设置为嵌入式或保留原样,将其设置为内容
像这样访问
private void button1_Click(object sender, EventArgs e) { SoundPlayer snd = new SoundPlayer( Properties.Resources.tada); snd.Play(); }
如果要通过在项目中播放.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
请记住,您必须使用正斜杠 (/)格式编写文件的路径,在给出文件路径时不要使用反斜杠 (),否则会出错