Playing wav file which has relative path inside project
Asked Answered
S

5

5

I have some problems with relative paths and reproduction of wav files. I have this simple code which works perfectly:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Users\Admin\Documents\Visual Studio 2012\Projects\TestProject\TestProject\Data\Sounds\car.wav";
player.Play();

I want somehow to play that file with relative path but I didn't have success with this:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"Data\Sounds\car.wav";
player.Play();

Thank you!

Shear answered 17/4, 2013 at 16:58 Comment(0)
C
6

Is Data directory in the root directory of your application? Are you copying the directory contents as output?

If so, did you mean, Data\Sounds\car.wav?

Which, if running from Visual Studio would be in [projectroot]\[release]\bin\Data\Sounds\car.wav

If you don't see this directory in your bin folder, you'll need to ensure you're selecting all of the files you want copied to your output directory (which will copy the directory structure). You can do this by clicking on the file in your project and selecting the file as output.

Citadel answered 17/4, 2013 at 17:0 Comment(4)
Yes, data is in the root directory.Shear
Not according to the file path that works in your question it isn't.Citadel
Hm ... I created new Data directory by adding new directory inside TestProject with right click on Project and 'Add new folder'. Isn't that a root directory? :)Shear
After compiling, your root is your bin\Debug or bin\Release directory.Citadel
F
2

Get the full path of your file with Path.GetFullPath("relativ/path")

Froemming answered 17/4, 2013 at 17:11 Comment(0)
B
2

You might be better off using absolute path after all. You can get the root path from the exe file, then append your relative path to it. Like this:

// getting root path
string rootLocation = typeof(Program).Assembly.Location;
// appending sound location
string fullPathToSound = Path.Combine(rootLocation, @"Data\Sounds\car.wav");
player.SoundLocation = fullPathToSound;
Bollworm answered 17/4, 2013 at 17:19 Comment(1)
This doesn't work for me - Assembly.Location contains the .EXE appended to the end, so I end up with bin\Debug\Program.exe\Data\Sounds which is wrong.Prunelle
G
1

This is worked for me

System.Media.SoundPlayer player1 = new System.Media.SoundPlayer(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\1.wav");
Gaffney answered 7/10, 2020 at 13:5 Comment(1)
Thanks for your contribution. Can you edit your answer to explain how this fixes the original question's problem?Unhair
M
0
   //WindowsFormsApplication4.exe is name of name space this file name found in Debug file

 //you should copy your "sound.wav" into your Debug file


      string x = (Assembly.GetEntryAssembly().Location + "");
      x = x.Replace("WindowsFormsApplication4.exe", "sound.wav");
      SoundPlayer player1 = new SoundPlayer(x);
      player1.Play(); 
Meagher answered 12/4, 2014 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.