playing .wav file with C#
Add your Wav file to resources by going to your Project Properties --> Resources Select Audio and Browse to the file. You will then be able to see it as part pf Propeties.Resources
. It will add it to a Resources Folder where you can set it to embedded or leave it as is, which is set as content
Accessed like this
private void button1_Click(object sender, EventArgs e)
{
SoundPlayer snd = new SoundPlayer( Properties.Resources.tada);
snd.Play();
}
If you want to add music in your program by playing your .wav file in projects. Then you have to add the .wav file like this.
using System.Media; // write down it at the top of the FORM
SoundPlayer my_wave_file = new SoundPlayer("F:/SOund wave file/airplanefly.wav");
my_wave_file.PlaySync(); // PlaySync means that once sound start then no other activity if form will occur untill sound goes to finish
Remember that you have to write the path of the file with forward slashes (/) format, don't use back slashes () during giving a path to the file, otherwise you will get an error
Currently I know two ways to do so, see below:
Use file path
First put the file in the root folder of the project, then no matter you run the program underDebug
orRelease
mode, the file can both be accessed for sure. Next use the classSoundPlayer
to paly it.var basePath = System.AppDomain.CurrentDomain.BaseDirectory; SoundPlayer player = new SoundPlayer(); player.SoundLocation = Path.Combine(basePath, @"./../../Reminder.wav"); player.Load(); player.Play();
Use resource
Follow below animate, add "Exsiting file" to the project.
SoundPlayer player = new SoundPlayer(Properties.Resources.Reminder);
player.Play();
The strength of this way compared to the other is:
Only the folder "Release" under the "bin" directory need to be copy when run the program.