How to Easily Play Wave Files with Visual Basic.NET and Newer

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

It’s actually quite easy to play wave files (.wav waveform based sound files) using just alittle code in some cases. The .NET Framework 2.0, 3.5, 4.0, and higher has a managed wrapper around the PlaySound API.

The easiest way to play wave files with Visual Basic 2005, VB 2008, VB 2010, and newer is as shown below…

    'Play a wave sound file from a specified location.

    'Stop the file from playing.

    'Play a Windows system sound.

The code above simply used the MY Interface for quick access to the Audio class to play a file, stop the playback, and then playback a system sound.

What about playing wave sound files with Visual Basic.NET 2002 and Visual Basic.NET 2003?

Well, playing waves with the original versions of Visual Basic.NET is alittle more involved since .NET Framework 1.0/1.1 does not include an integrated class. But its still not that hard to do. Its similar to VB 6.0 and will just take alittle more code and some API. Here are the Flags and Function…

    'Some of the flags you can use with the PlaySound API call.
    Private Const SND_APPLICATION As Int32 = &H80 'look for application specific association

    Private Const SND_ALIAS As Int32 = &H10000 'name is a WIN.INI [sounds] entry

    Private Const SND_ALIAS_ID As Int32 = &H110000 'name is a WIN.INI [sounds] entry identifier

    Private Const SND_ASYNC As Int32 = &H1 'play asynchronously

    Private Const SND_FILENAME As Int32 = &H20000 'name is a file name

    Private Const SND_LOOP As Int32 = &H8 'loop the sound until next sndPlaySound

    Private Const SND_MEMORY As Int32 = &H4 'lpszSoundName points to a memory file

    Private Const SND_NODEFAULT As Int32 = &H2 'silence not default, if sound not found 

    Private Const SND_NOSTOP As Int32 = &H10 'don't stop any currently playing sound

    Private Const SND_NOWAIT As Int32 = &H2000 'don't wait if the driver is busy

    Private Const SND_PURGE As Int32 = &H40 'purge non-static events for task

    Private Const SND_RESOURCE As Int32 = &H40004 'name is a resource name or atom

    Private Const SND_SYNC As Int32 = &H0 'play synchronously (default)
    'The API call that does the work of playing the sound for us.
    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszSoundName As String, ByVal hModule As Int32, ByVal dwFlags As Int32) As Int32

Now decide the way you want to playback your wave files. In other words, decide which flags you want to use. IF the wave file is a Filename then put the: SND_FILENAME constant in the dwFlags parameter of the PlaySound function. What if you wanted to play a file and make it loop over and over? Simply put the SND_FILENAME and SND_LOOP constants in the Playsounds dwFlags parameter. You would use multiple flags like this: (SNDFILENAME or SND_LOOP) in the same dwFlags parameter. Below is some code to play a sound file.

'Play the file without consuming or freezing the calling thread until it finished.
PlaySound("MYSoundPathAndFile.wav", Nothing, SND_FILENAME or SND_ASYNC)


'Play the .wav file and keep looping over and over.
PlaySound ("MYSoundPathAndFile.wav",Nothing, SND_FILENAME or SND_LOOP)


'Simply call PlaySound like its shown below to stop playing.
PlaySound (Nothing,Nothing,Nothing)

Thats all there is to it! With this API you can play system sounds, wave files from .ini listings, resources, sync or async, and so on.

Hopefully you got something as least helpful from this small post


Revised: 2015

3 thoughts on “How to Easily Play Wave Files with Visual Basic.NET and Newer

  1. Gaston Pouliot

    But how to stop a systemSound.
    We have My.Computer.Audio.PlaySystemSound()
    but not My.Computer.Audio.STOPSystemSound().
    Me I want no sound on the key enter of my keyboard.
    Thanks for your answer

  2. Jason H

    Hi, I am not sure why – My.Computer.Audio.Stop() – won’t stop a SystemSound that is playing. I may have to look into it alittle more to see if there is a solution when I get time.

    Your next question is already answered on this site. I have a brief article at: that will show you why and how to keep the Beep from occurring in a Single Line textbox control while pressing the “Enter” key.

    I also have a snippet for .net 02/03 that will keep the beep from taking place pressing the Enter key in case you use the earlier dotNet languages.


  3. bRonCOde

    Playsound with a resourced file is subject to memory leak, so careful.
    And the Garbage Collector won’t help you there (in my case it didn’t).


Leave a Reply