Audio Recorder - Select Recording Device - VB.Net

2.3k Views Asked by At

I'm trying to create an audio recorder using VB.Net.

This is how I'm doing it:

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

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

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

    mciSendString("set recsound samplespersec 11025 channels 2 bitspersample 16 alignment 4 bytespersec 44100", vbNullString, 0, 0)
    mciSendString("record capture", vbNullString, 0, 0)

    Label1.Text = "Recording..."

End Sub

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

    mciSendString("save recsound c:\recsound.wav", "", 0, 0)

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

    Label1.Text = "Stopped."

End Sub

It works fine, the only problem is that I have two mics, one of them is built-in and the other one is connected via USB. The second one has a way better recording quality but this application always records from the built-in mic.

I've searched all over the internet and I can't find a way to select the recording device. The only thing I was able to find was:

Dim DeviceId As Integer = 2

mciSendString("set recsound input " & DeviceId.ToString())

I've tried different values to no avail.

I have also tried the following code that successfully listed all the properties of all the recording devices found in my computer but I couldn't find anything that could help:

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

    Dim objSearcher As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice")
    Dim objCollection As System.Management.ManagementObjectCollection = objSearcher.Get()

    Me.TextBox1.Text = ""

    For Each obj As ManagementObject In objCollection

        Me.TextBox1.Text &= "---------------------------------" & vbCrLf

        For Each myProperty As PropertyData In obj.Properties

            Me.TextBox1.Text &= myProperty.Name & " - " & myProperty.Value & vbCrLf

        Next

    Next

End Sub

Any suggestions will be appreciated.

1

There are 1 best solutions below

0
On

I realize that you asked this question almost two years ago and so you may never see this answer. Perhaps you have notifications turned on and this may help you.

I have switched to Naudio (available through Nuget) for my recording applications and have had much better luck with it. I can specify the recording device as follows:

waveIn = New WaveIn waveIn.DeviceNumber = 0 '0 - Stereo Mix, 1 - Microphone, 2 - Line In

Figure out what device number your USB microphone is and use that. Do a search on "Naudio" and you will find example code.