Recording Sounds from Visual Basic

Introduction

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.

MCI

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:

Sounds
Figure 1: Our design

Code

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)

      Me.ComboBox1.Items.Add(Me.SoundName)

   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", _
      AudioPlayMode.Background)

   End Sub

The preceding code simply plays the recorded sounds.

Conclusion

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

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read