音频文件路径不是动态的

Audio file path not dynamic

我需要在应用程序运行时播放音频文件并且它成功地做到了这一点,唯一的问题是当我将应用程序移动到另一台 PC 时,声音将无法播放,因为文件路径不正确return 真了。我该如何解决这个问题?

namespace Dewey_Decimal_System
{
    public partial class Dewery_Decimal_System : Form
    {
        private int _ticks;

        public Dewery_Decimal_System()
        {
            InitializeComponent();
            Second();
            timer1.Start();
            

            SoundPlayer splayer = new SoundPlayer(@"C:\Users\Luke Warren\Desktop\Prog3B\Dewey Decimal 
            System\Media\EntroMusic2.wav");
            splayer.Play();


            panel1.Hide();
            button1.Hide();
            button2.Hide();
            button3.Hide();
            this.Hide();

        }

你会做这样的事情来获取应用程序环境文件夹中的路径“Data\ich_will.mp3”。

string fileName = "ich_will.mp3";
string path = Path.Combine(Environment.CurrentDirectory, @"Data\", fileName);

在我的例子中,它将 return 如下:

C:\MyProjects\Music\MusicApp\bin\Debug\Data\ich_will.mp3

  • 最快的方法:使用SoundPlayer splayer = new SoundPlayer(@"EntroMusic2.wav"); 并将文件“EntroMusic2.wav”移动到与应用程序 exe 文件相同的文件夹中。
  • 理想方式:将此文件路径移动到配置文件中,而不是对其进行硬编码。

我也遇到了同样的问题,目标电脑音频路径报错是在家用电脑!我搜索 google 没有人有解决方案都说同样的话“把它放在资源中,制作内容。嵌入资源,输出目录和许多答案,但没有任何效果!它仍然错误显示文件丢失,文件路径来自家用电脑。 我被迫将所有音频文件放在单独的 mapp 中并将其发送给朋友,并告诉他将 mapp 放在他的电脑上的 C:\GameAudio 下并将程序中的所有音频更改为新位置 t.ex C :\GameAudio\hit.wav.