Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a question, I created a WPF application that has an audible signal when the user answers right or wrong (the audio track is different for a particular answer). But my path set to the file will only work on my computer, unless the user has the same saved file. Is there any way to solve this, for example, that the soundtrack is wrapped in a program or something?

What I have tried:

private void soundEffect(bool correctnessOfAnswer)
       {
           string path;
           if (correctnessOfAnswer == true)
           {
               Uri path = new Uri(@"pack://application:,,,/Resources/correct sound effect.wav");
           }
           else
           {
               Uri path= new Uri(@"pack://application:,,,/Resources/Buzzer Wrong Answer - Gaming Sound Effect(HD).wav");
           }
           MediaPlayer player = new MediaPlayer();
           player.Open(path);
           player.Play();
       }


This is a method of playing an audio track that also shows the path to the file.

Thank you for any advice
Posted
Updated 19-May-21 23:24pm
v3

I was able to find an example of embedding sound files just by doing a quick Google: c# - Playing sounds in WPF as a resource - Stack Overflow[^] You need to embed the sound file as a resource in the executable, and then use the resource in the sound player. The alternative is to have the sound files included with the installer for the application and extracted to some common directory, or even the directory where the executable is being stored.
 
Share this answer
 
Comments
Member 15170612 20-May-21 5:19am    
I try it according to the advice and it doesn't work
Chris Copeland 20-May-21 5:39am    
What doesn't work, are you getting an exception or is the sound just not playing? The update you provided to your question doesn't match the example that I provided in the link I sent, instead it seems to be using the example from the question itself where the author says that using the new Uri() and MediaPlayer doesn't work.
Member 15170612 20-May-21 6:12am    
Okay, my thinking sometimes is on ... :D Thank you very much!!!
The other computer did not se that audio files are in the Debug. It just se the file Path from the Code.
I solved the problem by making new folders as it written in the error. and put the audio files there.
The error said "kan not find ((C:\\Game\Debug\audio\ad.wav)). but this path is from the first computer where i made the program. So in the other computer which i want to test my program! I made new folders exct the same path ..... in C:\\ i made folder Game and inside it a folder named it Debug and inside it a new folder called it audio and ther i put the audio ad.wav and start the program it worked!!!!! Really strange?????? Why Visuall studio did not include only the file name only ad.wav why the take the whole path in the CODE!!!!!
SoundPlayer so = new SoundPlayer();
so.SoundLocation=(@"C:\Game\Debug\audio\ad.wav");
so.Play();..........................................this works........
it would be better if only the file name ("ad.wav")...........this dont works...........WHYYYYYYYYY?
 
Share this answer
 
Comments
OriginalGriff 3-May-22 14:47pm    
Because your code specifies an absolute path:
so.SoundLocation=(@"C:\Game\Debug\audio\ad.wav");

That tells the system "I know better than you, I know the file is in this exact folder".
Which is silly, because in production it will be installed under the "Program Files" folder (which is a protected folder for antivirus reasons).

So when you duplicate the folder path so it matches the path you specified it works. Maybe - it will depend on permissions applied to that folder path when you created it compared to the permissions set by the installer or user.

A better idea is to store your data in a "safe place" that is user specific. This may help: Where should I store my data?[^] for some better ideas.
Richard Deeming 4-May-22 3:55am    
Your question is not a "solution" to someone else's question!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900