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

Posted by: Suprotim Agarwal , on 2/22/2008, in Category WinForms & WinRT
Views: 63748
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.
 
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 a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



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.

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook