简体   繁体   English

如何播放应用程序文件夹中的声音?

[英]How can I play a sound that will be in the application folder?

I am trying to make that when the program initialize it plays a sound, but when I pass the installer to someone else it doesn't work since the destination is not correct in their computer. 我试图在程序初始化时发出声音,但是当我将安装程序传递给其他人时,由于目的地在他们的计算机中不正确,因此它不起作用。

How can I refer to the file in a way that will work in others computer? 如何以在其他计算机上可以使用的方式引用文件?

Here is the code that I used: 这是我使用的代码:

public partial class Form1 : Form
{
    private SoundPlayer unacabeza = new SoundPlayer();

    public Form1()
    {
        InitializeComponent();
        unacabeza.SoundLocation = @"c:\Por_una_Cabeza_-_Carlos_Gardel_Gcxv7i02lXc.wav";
        unacabeza.Play();
    }
}

您可以将wav文件放在执行文件夹中,然后仅用unacabeza.SoundLocation = @"Por_una_Cabeza_-_Carlos_Gardel_Gcxv7i02lXc.wav";替换代码unacabeza.SoundLocation = @"Por_una_Cabeza_-_Carlos_Gardel_Gcxv7i02lXc.wav";

you can put the wav file in the Resources Folder when you do that you don't have to worry about the location or file delete by user ,you can done that by 您可以将wav文件放到资源文件夹中,而不必担心用户的位置或文件删除,可以通过以下方式完成操作:

solution Explore > Properties > double click on Resources then a new tab will open, drag and drop your wav file into it 解决方案探索>属性>双击资源,然后将打开一个新选项卡,将wav文件拖放到其中

and rebuild the project 并重建项目

to play the wav file from the resources all you need is 资源中 播放wav文件,您所需要做的就是

SoundPlayer test= new SoundPlayer(Properties.Resources.'filename');
test.play();

or if you just need to play the wav from the app folder you can 或者,如果您只需要从app文件夹播放wav ,则可以

  • SoundPlayer test = new SoundPlayer(Application.StartupPath + "\\\\filename.wav");
  • SoundPlayer test = new SoundPlayer(".\\\\filename.wav");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM