How to play audio files(.wav) asynchronously using Windows Forms

Posted by: Suprotim Agarwal , on 2/22/2008, in Category WinForms & WinRT
Views: 71213
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.

Absolutely Awesome Book on C# and .NET

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!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
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 has received the prestigious Microsoft MVP award for ten consecutive times. 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



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Alex Cruz on Tuesday, May 13, 2008 1:35 PM
Excellent article....can't wait for the other articles on videos and hope you include one for mp3!!!!
Comment posted by Joanne on Sunday, July 5, 2009 1:42 PM
thanks! this article really helped a lot! more power!
Comment posted by oliyavan on Tuesday, November 24, 2009 8:42 AM
hi, i want to combine two or more .wav files.
pls help me.
Comment posted by sriram on Sunday, February 28, 2010 4:59 AM
how to make this player play mp3 files?
Comment posted by ancolo on Wednesday, April 21, 2010 12:54 PM
pro
Comment posted by anamika on Thursday, July 22, 2010 5:06 AM
wonderfull!!coild u plz tell us how can we do this using sql database.
Comment posted by ana on Friday, July 23, 2010 8:05 AM
You have done really a wonderfull job.please tells us how to play vedio files in asp.net.please its urgent.i have gone thru several sites but no body explain briefly.plz.. help
Comment posted by nguyen thi an on Sunday, September 5, 2010 6:32 AM
do not set for openFileDialog is 'WAV Files|*wav'. You can empty
Comment posted by asdfasdf on Monday, November 1, 2010 7:22 AM
sdfasdf
Comment posted by Guesmi on Wednesday, April 9, 2014 3:40 AM
Sound API only supports playing PCM wave files.