Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\mywavfile.wav");
player.Play();

or You can embed the sound files into your project using resources.

System.Media.SoundPlayer player = new System.Media.SoundPlayer(Resources.Yourfile);
player.Play();

Update: When you add a sound file to the Resources, you can access it by the name of the file.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top