0

I'm writing a program to play audio from a Resources folder located in the same directory as my application (so it can be moved to a different computer) using the Windows Media Player component.

My current code to select the audio clip is:

soundPlayer.URL = @"C:\Users\Ryan\Desktop\MyProgram\SFX\nameThatTune.mp3";

Which works fine, but when I change it to:

soundPlayer.URL = @"SFX\showIntro.mp3";

It stops working. I've also tried beginning the file path with ~/ and ../, but neither work. Is this a problem specific to the WMP Component or am I missing a bigger problem?

svick
  • 236,525
  • 50
  • 385
  • 514
Ryan
  • 29
  • 1
  • 5
  • If it is in current folder, try `./` prefix. I may also depend on where the executable is located. – Shimon Rachlenko Feb 24 '13 at 09:03
  • Never put your projects on your desktop! Be neat and create specific folders for your specific categories of files and document! :-) – Mohammad Dehghan Feb 24 '13 at 09:23
  • I actually have them a bit more organized, but I moved it around to shorten the path for the sake of making the post. I'm debugging it for now, but will the code need to be changed once I publish it to an executable? – Ryan Feb 25 '13 at 08:15
  • If you put the file in project's root folder, using `@".\SFX\showIntro.mp3"` should work. But it is better to put the file in the output directory (`bin\Debug` for example). Then you can use my answer and nothing need to be changed. – Mohammad Dehghan Feb 25 '13 at 09:47

1 Answers1

1

Relative paths depend on the value of Environment.CurrentDirectory. When you run the application inside Visual Studio, the current directory points to your project's root folder (not the application's executalbe path). Also some operations can change the current directory (opening a file using OpenFileDialog). You can't always rely on the current directory to be a specific place, so you should alwasy try to use absolute path.

If the file is in the applications folder (near the .exe file), you can use:

soundPlayer.URL = Path.Combine(
    Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
    "showIntro.mp3");

or if you are on WinForms:

soundPlayer.URL = Path.Combine(
    Path.GetDirectoryName(Application.ExecutablePath),
    "showIntro.mp3");

Changing it to point to a sublfolder is also straightforward.

Notice that the file must in the output folder of your project (bin\Debug for example).

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
  • Unfortunately, I get an error (like the red underline) when I try to use Path and Assembly. Any idea why? – Ryan Feb 25 '13 at 08:07
  • @Ryan Yes I have (ideas)! You should add respective `using` statements in your code file. Click on the word with red line under it (`Path` for example), then you see a small blue box under the word. Press `Ctrl+.` (Control key and then dot key). Then select the first item in the menu (`using System.IO` for example). You can also do it with mouse. This way VisualStudio automatically adds the required `using` statement. – Mohammad Dehghan Feb 25 '13 at 08:13
  • Okay, I did that and the line is gone, however it's still not playing. I tried modifying the file path to "SFX\nameThatTune.mp3" and "SFX/nameThatTune.mp3", but to no avial. Any other ideas why this isn't working? Thanks!! – Ryan Feb 25 '13 at 08:36
  • @Ryan Place a break point on the line after setting the `URL` and inspect the value of `URL` property. What is it? – Mohammad Dehghan Feb 25 '13 at 08:55
  • (If I inspect this>soundPlayer?URL in the Locals window, right? (Sorry, I'm fairly new to C#) – Ryan Feb 25 '13 at 09:06
  • @Ryan You can easily hover the mouse over the `URL` property and see its value. I think the value still is not set. After break-point is hit, press `F10` to skip one statement, then see the value of `URL`. It should be the path to your file. I think you need to learn more about C#. Read a book please. – Mohammad Dehghan Feb 25 '13 at 09:11
  • Okay, the URL is "C:\\Users\\RYAN\\Desktop\\RadioShow\\MyProgram\\MyProgram\\bin\\Debug\\Radibro.EXE\\SFX\\showIntro.mp3" I've taken a class and written several programs, I just don't have much experience debugging, sorry. – Ryan Feb 25 '13 at 09:21
  • That works! Perfect! One last question: what do I need to do to make sure my resources stay in the right output folder when I publish the program to use on another computer? – Ryan Feb 25 '13 at 19:52
  • Nothing special! You just need to deploy your files. If you are creating installer for your application, make sure that you put your files in correct folders. If you use xcopy deployment (you simply copy your files), again, put the files in correct folders :-) One more thing: If the answer solves your problem, you can mark it as accepted answer. Please read [faq] for more information. – Mohammad Dehghan Feb 26 '13 at 03:16