Search code examples
c#audiorelative-pathsolution-explorersoundplayer

Play a sound using SoundPlayer


I'm trying to play a specific sound. However I can't seem to reach the file that I added in my Solution Explorer. (I have this folder called "Sounds/" with several .wav-soundeffects in it)

When I hardcode a filepath to a random fixed location on my HDD it works just fine. Also when I put my resources in my "bin/debug/"-folder it works just fine (this is the closest I got to a working relative path with the AppDomain.CurrentDomain.BaseDirectory function).

How can I make my SoundPlayer work when using the files I added in my Solution Explorer?


Solution

  • You could embed them as a project resource on the dll/exe and then use them through the Application object:

    Adding and Editing Resources (Visual C#)

    http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.90%29.aspx

    You could also use the Resource Manager (better option if you want to have your sound files delivered separately):

    Resource Manager

    http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager%28v=vs.110%29.aspx