문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top