Play Sound Using SoundPlayer

I am trying to play a specific sound. However, I cannot find the file that I added in my solution explorer. (I have this folder called "Sounds /" with several .wav-soundeffects in it)

When I hard-link a path to a random fixed location on my hard drive, it works fine. Also, when I put my resources in my bin / debug / folder, it works just fine (this is the closest way to the working relative path using the function AppDomain.CurrentDomain.BaseDirectory).

How can I make my SoundPlayer work by using the files that I added in my solution explorer?

+4
source share
2 answers

You can embed them as a project resource in 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 can also use the resource manager (the best option if you want your audio files to be delivered separately):

Resource manager

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

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

or you can embed sound files in your project using resources.

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

Update. When you add an audio file to resources, you can access it by file name.

+2
source

Source: https://habr.com/ru/post/1529383/


All Articles