Recording Sounds from Visual Basic


What would the world be without sounds? I sit on my porch and listen to the birds and try to imagine a world without the sounds of birds chirping, kids laughing, and the sound of music. Before I get nostalgic, let me tell you that today's article topic is recording sounds with Visual Basic.

Different options available in working with sounds in .NET. I'll describe them in the upcoming sections.

The Waveform Audio Interface

The Waveform Audio Interface provides several functions for controlling the hardware audio interface and provides the lowest level of control available to Windows Mobile developers. Please read the preceding article for more information.

mciSendString API

The mciSendString function sends a command string to an MCI device. The device that the command is sent to is specified in the command string.

I have always loved the Windows API. It might seem odd, looking at this API's name, that this API would be able to record sounds, but it works a bid differently than what you think. Here is more information on the mciSendString API.


The Media Control Interface (MCI) provides standard commands for playing multimedia devices and recording multimedia resource files. These commands are a generic interface to nearly every kind of multimedia device.

Our Project

Star a new Visual Basic Windows Forms project and design it as shown in Figure 1:

Figure 1: Our design


Add the mciSendString API to your code:

   Private Declare Function mciSendString Lib "winmm.dll" _
      Alias "mciSendStringA" (ByVal lpstrCommand As String, _
      ByVal lpstrReturnString As String, _
      ByVal uReturnLength As Integer, _
      ByVal hwndCallback As Integer) As Integer

Declare a modular variable to hold the Sound's name:

   Dim SoundName As String

Add the code to record the sound from a microphone behind Button1's click event:

   Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click

      ' Record from microphone

      mciSendString("open new Type waveaudio _
         Alias recsound", "", 0, 0)

      mciSendString("record recsound", "", 0, 0)

   End Sub

Here, I send two commands. The first instruction starts a new session to be recorded. The second instruction records the physical input from the microphone.

Add the following code behind Button2:

   Private Sub Button2_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button2.Click

      Me.SoundName = InputBox("Your sound name")

      ' stop and save new recording

      mciSendString("save recsound c:\" & _
         Me.SoundName & ".wav", "", 0, 0)

      mciSendString("close recsound", "", 0, 0)


   End Sub

Two instructions are being sent again. The first instruction saves the sound that was input. The second command closes the recording session. Lastly, the input sounds get added to a combobox.

Add the following code behind the combobox's SelectedIndexChanged event:

   Private Sub ComboBox1_SelectedIndexChanged(ByVal sender _
      As System.Object, ByVal e As System.EventArgs) _
      Handles ComboBox1.SelectedIndexChanged

      My.Computer.Audio.Play("c:\" & _
      Me.ComboBox1.SelectedItem & ".wav", _

   End Sub

The preceding code simply plays the recorded sounds.


And you thought recording sounds would be difficult, didn't you? As you can see, it is actually quite easy. Until next time, cheers!

This article was originally published on November 16th, 2015

About the Author

Hannes DuPreez

Hannes du Preez is a self-taught developer who started learning to program in the days of QBasic. He has written several articles over the years detailing his programming quests and adventures. .NET is his second love, just after his wife and kid. He has always been an avid supporter of .NET since the beginning and is an expert in VB and C#. He was given the Microsoft Most Valuable Professional Award for .NET (2008–2017). He has worked as a moderator and an article reviewer on online forums and currently works as a C# developer and writes articles for CodeGuru.com, Developer.com, DevX.com, and the Database journal.
His first book Visual Studio 2019 In Depth is currently on sale on Amazon and Bpb Publications.

You could reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date