用C#播放.wav文件

Arc*_*ade 5 c#

我正在尝试播放位于项目内部文件夹中的.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();
Run Code Online (Sandbox Code Playgroud)

我无法播放声音,我不断发出声音,因为找不到声音.

堆栈溢出的某处我发现这段代码可以找到正确的汇编路径.

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

                MessageBox.Show(resourceNames[i]);


            }
Run Code Online (Sandbox Code Playgroud)

它输出这两条路径

kisscam.Properties.Resources.resources
Run Code Online (Sandbox Code Playgroud)

kissscam.g.resources
Run Code Online (Sandbox Code Playgroud)

我尝试过使用它们,但它们都不起作用.

谁知道我做错了什么?

Mar*_*all 9

通过转到项目属性 - >资源选择音频并浏览到该文件,将您的Wav文件添加到资源.然后,您将能够将其视为pf的一部分Propeties.Resources.它会将它添加到资源文件夹中,您可以将其设置为嵌入式或保留原样,将其设置为内容

在此输入图像描述

像这样访问

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

}
Run Code Online (Sandbox Code Playgroud)


Pir*_*hah 5

如果要通过在项目中播放.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
Run Code Online (Sandbox Code Playgroud)

请记住,您必须使用正斜杠(/)格式写入文件的路径,在为文件提供路径时不要使用反斜杠(),否则会出现错误