Trouble pathing an audio file using WMPLib

109 views Asked by At

I'm new to C# and pathing files have troubled me for a while.

The file structure to the audio files is as follows:

C:\Users\Username\Documents\Program\Form\WindowsFormApp\Audio Files\name.mp3

My visual studio forms are in:

C:\Users\Username\Documents\Program\Form\WindowsFormApp

I tried referencing audio files by using the following URL:

soundplayer.URL = @"Audio Files\name.mp3";

However, that did not work.

I then placed the folder Audio Files inside practically every other folder. It still did not work.

I tried:

soundplayer.URL = @"..\\Audio Files\name.mp3";

That did not work as well. I cannot simply do the entire path of the audio because it is different on each computer.

How do I path this correctly?

1

There are 1 answers

3
Héctor M. On BEST ANSWER

Well, in your case, it would be as follows

using System.IO;
using WMPLib;

WindowsMediaPlayer player;
private void button1_Click(object sender, EventArgs e)
{
 string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Audio Files\name.mp3");
 player = new WindowsMediaPlayer();
 FileInfo fileInfo = new FileInfo(path);
 player.URL = fileInfo.Name;
 player.controls.play();
}

Where

//This function gets the current route of your project and combines it with the subfolder path where your music file is
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Audio Files\name.mp3");