0

Situation

this is probably a really simple question with a really simple answer, but I can't find the answer anywhere, so I'm going to ask here, I've been working around this because it isn't a huge issue for me, but it will be when I submit my work.

Problem

I have a soundplayer, it works fine on my PC and does exactly what I want it to do on this PC.

SoundPlayer player = new SoundPlayer(@"C:\Users\Font\Desktop\GRADED_UNIT_SOLUTION_PLANNING_UPDATED\HillRacingGraded\HillRacingGraded\Resources\Audio\" + track + ".wav");

The problem is the path.

because of THIS path my program won't work on ANY other System apart from the one it's working on right now. It crashes soon after startup.

And.. as you can probably see from the path, it's going to eventually be graded, so my lecturer will need to use this program without having to switch around a directory.

How can I get the Soundplayer path to start at "HillRacingGraded\ ...\ ..."?

Rather than it starting at the C: drive.

Method
  • 143
  • 9
  • possible duplicate of [Getting path relative to the current working directory?](http://stackoverflow.com/questions/703281/getting-path-relative-to-the-current-working-directory) – goobering May 11 '15 at 01:47

2 Answers2

0

If you're using System.Media.SoundPlayer, it supports reading of sound files from streams and I've seen it successfully used in the past from the UnmanagedResourceStream you get from an embedded resource. So one option would be to embed your sound file as a resource in your application and play it from there.

If embedding the file isn't an option, you can get paths relative to your executable folder using code similar to that shown below. Then you just have to provide your executable and the resource files in a subdirectory (but be careful with GetExecutingAssembly() if there's any chance your code is in a DLL and can be hosted by an arbitrary executable).

var assembly = Assembly.GetExecutingAssembly();
var folder = Path.GetDirectoryName(assembly.Location);
var soundFile = Path.Combine(folder, "MySoundFile.wav");

The special folder path helpers can also reduce the hard-coding but work best when your application will be installed via an installer and you can put files in the appropriate places automatically (which isn't likely to be the case for a school project).

ScheuNZ
  • 911
  • 8
  • 19
  • I have a folder in my solution that contains audio and image resources. i was hoping I could just use the path Resources/Audio/file than the entire path of my machine. – Method May 11 '15 at 15:40
  • @Method, you can use Folder\OtherFolder\File.wav but it relies on the working directory being set in order to work correctly and so is unreliable (even though the default is to use the executable directory). As long as your sound files are set to 'Copy always' or 'Copy if newer' they will be copied to your output folder and can be accessed with a path relative to your executable folder. My other suggestions are only there because they are more reliable and don't break if the working directory changes. – ScheuNZ May 11 '15 at 19:40
0

For me (VS 2022, .net 6, C # 10) it worked:

  1. Import the "file.wav" file into the main directory.

  2. Change in: Properties (file.wav) - Copy to Output Directory to: Copy always. Later it was enough to:

     SoundPlayer player = new SoundPlayer("file.wav");
     player.Load ();
     player.Play ();
    
RB1
  • 1