How to play audio files(.wav) asynchronously using Windows Forms
Posted by: Suprotim Agarwal ,
on 2/22/2008,
in
Category WinForms & WinRT
Abstract: In this article, we will explore how to play audio files (.wav) asynchronously using Windows Forms. We will be making use of the System.Media.SoundPlayer class which controls playback of a sound from a .wav file.
How to play audio files(.wav) asynchronously using Windows Forms
In this article, we will explore how to play audio files (.wav) asynchronously using Windows Forms. We will be making use of the System.Media.SoundPlayer class which controls playback of a sound from a .wav file. Let us get started.
Step 1: Create a new windows application. Open Visual Studio > File > New > Project > Windows Application > Rename it to ‘WindowsPlayAudio’.
Step 2: Drag and drop a label, 2 button controls and an OpenFileDialog component to the form. Rename them as follows :
Label1 – lblFile
Button1 – btnOpen
Button2 – btnPlay
TextBox – txtFileNm
OpenFileDialog – Set the Filter to ‘WAV Files|*.wav’
In the Form1.cs, add the following namespace
C#
using System.Media;
VB.NET
Imports System.Media
Step 3: On the ‘btnOpen’ click, display File Open dialog box and accept the selected .wav file in the txtFileNm textbox
C#
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtFileNm.Text = openFileDialog1.FileName;
}
VB.NET
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
txtFileNm.Text = openFileDialog1.FileName
End If
Step 4: Code the btnPlay click event to play the selected file asynchronously
C#
private void btnPlay_Click(object sender, EventArgs e)
{
if (txtFileNm.Text != String.Empty)
{
SoundPlayer wavPlayer = new SoundPlayer();
wavPlayer.SoundLocation = txtFileNm.Text;
wavPlayer.LoadCompleted += new AsyncCompletedEventHandler(wavPlayer_LoadCompleted);
wavPlayer.LoadAsync();
}
}
private void wavPlayer_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
((System.Media.SoundPlayer)sender).Play();
}
VB.NET
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As EventArgs)
If txtFileNm.Text <> String.Empty Then
Dim wavPlayer As SoundPlayer = New SoundPlayer()
wavPlayer.SoundLocation = txtFileNm.Text
AddHandler wavPlayer.LoadCompleted, AddressOf wavPlayer_LoadCompleted
wavPlayer.LoadAsync()
End If
End Sub
Private Sub wavPlayer_LoadCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
CType(sender, System.Media.SoundPlayer).Play()
End Sub
Step 5: That’s it. Run the application (F5). Click the Select File button and choose a .wav file. Click on the play button to play the file asynchronously.
In the coming articles, we will explore how to play video files asynchronously in windows forms. I hope you liked the article and I thank you for viewing it.
If you liked the article, please subscribe to my RSS feed over here.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn