Tag Archives: sound

How to Play an Embedded Resource .Wave Sound in VB.NET 2008, Visual Basic 2010, and Newer

I’ve recenty been making an example program with Visual Basic.NET 2008 on snapping forms to the edge of the screen. I wanted the program to play a sound wave file when it performed the snapping process. But I didn’t want to have to make it link to external wave files or anything. So I decided to embed the wave sound bytes in the program itself. So this little article will show you how I went about doing that. Here is a link to the actual example application that this article is based on.

Example Playing Embedded Wave Sounds

More Info

The first thing you want to do of course is determine the wave files you want to play in your application. Remember that the size of each .wav file in bytes, willl be added to your programs file size. So remember that when you decide on what sound files you want to use.

I also want to mention that this article is specific to VB .NET 2005/2008/2010 and higher since I am using the SoundPlayer class. Visual Basic .NET 2002/2003 will have to use the PlaySound api thats part of the WinMM.dll library. The PlaySound api can play and do everything the SoundPlayer class in VB 2005 and higher can do. Just more code is involved. I actually found an article after I started this post that shows how to play wave files embedded in your VB.NET 02/03 application. Just click here to check it out.

Embedding your .Wave Files

After you have determined the wave files you want to embed in your program you need to add them to your project. There are a couple ways to do this. The easiest way is to copy your .wav file and then paste it in your project. To paste it to your project you just need to select your project name in the Solution Explorer, usually the top right panel. Then right click and click on the paste command. Another way is to click on the “Project” and click on “Add Existing Item”. Then just browse to the wave files location and select each file you want to embed. Once the files are added to your project, you will want to click on each wave file and in the properties panel (Its right below the Solution Explorer by Default). In the Properties panel you will see “Build Action”. From the build action combo list select “Embedded Resource”. Do that for each sound file you added to your project.

Playing your Embedded Files

Now that you have embedded your files into your application, its time to setup the code to play them. If your using .NET older than 2005 then check out this article on using the unManaged api call “PlaySound” to do the playback.

You now need to access your newly embedded *.wav file. You can access it as a stream under your programs manifest. Here is the method that will be used…

Public Overridable Function GetManifestResourceStream(ByVal name As String) As System.IO.Stream Continue reading

How to check if a computer has a playback or input Soundcard in VB and VB.NET

Thankfully, Microsoft has a couple simple little API calls that will determine how many playback devices the target computer has and how many audio input devices for recording. The API calls are listed below…

Visual Basic.NET, Visual Basic 2008, VB 2010, VB 2013

   Private Declare Function waveOutGetNumDevs Lib "winmm.dll" Alias "waveOutGetNumDevs" () As Int32

    Private Declare Function waveInGetNumDevs Lib "winmm.dll" Alias "waveInGetNumDevs" () As Int32

Then you simply need to call those 2 functions and check what the retturn value. The return value will be how many devices on the target computer with playback and input capabilities. And if it happens to return a 0, then the computer does not have any…

        Dim audioPlaybackDevices As Int32

        Dim audioInputDevices As Int32

        audioInputDevices = waveInGetNumDevs

        audioPlaybackDevices = waveOutGetNumDevs

        MsgBox("This computer has: " & audioPlaybackDevices.ToString & " playback devices")

        MsgBox("This computer also has: " & audioInputDevices.ToString & " recording devices")

Visual Basic 6.0

VB 6.0 uses the same functions as .NET but with Long types instead of Integer.

    Private Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long

    Private Declare Function waveInGetNumDevs Lib "winmm.dll" () As Long


        Dim audioPlaybackDevices As Long

        Dim audioInputDevices As Long

        audioInputDevices = waveInGetNumDevs

        audioPlaybackDevices = waveOutGetNumDevs

        MsgBox("This computer has: " & audioPlaybackDevices & " playback devices")

        MsgBox("This computer also has: " & audioInputDevices & " recording devices")

That’s all there is to it for VB Classic and for Visual Basic.NET.     🙂


Minor Revision: 2015

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

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?

Continue reading

Play and Stop a Wave File or System Sound using VB 2005 and Higher

This shows how to use Visual Basic.NET 2005, VB 2008, Visual Basic 2010, and 2013 to play a wave (.wav) file and/or play a Windows SystemSound. This uses the MY interface which uses the SoundPlayer class new to .NET 2.0.

        'This will Play a Wave file from the specified Path, Play a selected system Sound and Stop a sound that
        'is playing.
        My.Computer.Audio.Play("C:\WINDOWS\Media\Windows XP Default.wav", AudioPlayMode.Background)