Play Audio (.WAV) Files in C#
Submitted by donbermoy on Monday, November 9, 2015 - 11:17.
In this tutorial, I will teach you how to create a program that will only play audio files preferably .wav files using c#.
So, now let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application.
2. Add two buttons; the browse button for locating audio files and the play button to play the file, and one textbox that displayed the filename of the audio file.
3. Now, have this code below.
Import first the namespace of Media to access SoundPlayer function to play the audio file.
Now in button1, we will have an openfiledialog that will trigger to open an audio file. And after choosing a audio file, it will display its filename in the textbox.
Lastly, this code is for button2 that will trigger to play the audio file.
Output:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

- using System.Media;
- private void button1_Click(object sender, EventArgs e)
- {
- if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- textBox1.Text = openfiledialog1.FileName;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- // play the audio file
- audio.SoundLocation = textBox1.Text;
- audio.Load();
- audio.Play();
- }
- // if not a .wav file, display the error message
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }

Add new comment
- 231 views