Virtual Developer Workshop: Containerized Development with Docker


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!

About the Author

Hannes DuPreez

Hannes du Preez is an ex MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic and C#. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles


  • Can You Help Me?

    Posted by Mateus on 02/13/2017 11:33am

    I did your code on my project but the last part isn´t working: "My.Computer.Audio.Play("c:\" & _ Me.ComboBox1.SelectedItem & ".wav", _ AudioPlayMode.Background)" Can you help me?

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

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