录音机 - 选择录音设备 - VB.Net

时间:2016-02-06 20:44:00

标签: vb.net audio-recording mci mcisendstring

我正在尝试使用VB.Net创建录音机。

我正是这样做的:

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

工作正常,唯一的问题是我有两个麦克风,其中一个是内置的,另一个是通过USB连接的。第二个有更好的录音质量,但这个应用程序总是从内置麦克风录制。

我在互联网上搜索过,但我找不到选择录音设备的方法。我唯一能找到的是:

Dim DeviceId As Integer = 2

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

我尝试过不同的价值无济于事。

我还尝试了以下代码,它成功列出了我计算机中找到的所有录制设备的所有属性,但我找不到任何有用的内容:

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

任何建议都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我意识到你差不多两年前就问过这个问题了,所以你可能永远不会看到这个答案。也许您已启用通知,这可能会对您有所帮助。

我已经为我的录音应用程序切换到Naudio(可通过Nuget获得)并且运气好得多。我可以按如下方式指定录音设备:

waveIn = New WaveIn waveIn.DeviceNumber = 0'0 - 立体声混音,1 - 麦克风,2 - 线路输入

找出USB麦克风的设备编号并使用它。搜索“Naudio”,你会找到示例代码。