Home > Back-end >  Audio file path not dynamic
Audio file path not dynamic

Time:10-03

I need the audio file to played when the app runs and it does this successfully, the only problem is is when I move the application to a different PC, the sound won't work because the file path doesn't return true any more. How do I fix this?

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();

        }

CodePudding user response:

You would do something like this to get the path "Data\ich_will.mp3" inside your application environments folder.

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

In my case it would return the following:

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

CodePudding user response:

  • Fastest way: Just use SoundPlayer splayer = new SoundPlayer(@"EntroMusic2.wav"); And move file "EntroMusic2.wav" into the same folder with the application exe file.
  • Ideal way: Move this file path into a config file instead of hard code it.
  • Related