0

正如标题所说,我正在尝试使用 vb.net 2010 和 NAudio 库实时存储和播放麦克风数据。我从 NAudio Codeplex 网站获得了 C# 代码,并在 vb.net 中进行了翻译。代码中没有错误并且程序可以正常工作(我放置了一个计数器,每次麦克风数据可用时都会不断增加和更新标签,以查看它是否真的在工作),但我什么也听不到。

这是代码

进口 NAudio
导入 System.IO 
'我正在使用的库

Private Sub wi_dataAvailable(ByVal obj As Object, ByVal e As Wave.WaveInEventArgs) 处理 wi.DataAvailable count += 1 '这里是计数器增加的地方 Label1.Text = count '并且这里的标签被更新,它似乎工作正常 play_packet(e.Buffer) 结束子

Private Sub play_packet(ByVal DR() As Byte) Dim MS As New MemoryStream(DR) Dim frmt As New Wave.WaveFormat frmt = Wave.WaveFormat.CreateALawFormat(8000, 1) Dim rsws As New Wave.RawSourceWaveStream(MS, frmt) Dim pcms As Wave.WaveStream = Wave.WaveFormatConversionStream.CreatePcmStream(rsws) Dim m_bwp As New Wave.BufferedWaveProvider(New Wave.WaveFormat(8000, 16, 1)) Dim dec() As Byte = cnssb(pcms) m_bwp.AddSamples(dec, 0, dec.Length) Dim latency As Integer Dim cbi As Wave.WaveCallbackInfo = Wave.WaveCallbackInfo.NewWindow Dim out_dev As New Wave.WaveOut(cbi) out_dev.DesiredLatency = latency out_dev.Init(m_bwp) out_dev.Play() End Sub Private Function cnssb(ByVal nss As Wave.WaveStream) As Byte() Dim memstr As New MemoryStream Dim buff(1024) As Byte Dim bytes As Integer bytes = nss.Read(buff, 0, buff.Length) While bytes > 0 memstr.Write(buff, 0, bytes) bytes = nss.Read(buff, 0, buff.Length) End While Dim by() As Byte = memstr.ToArray Return by End Function

希望你能帮我!

4

2 回答 2

0

每次收到记录的缓冲区时,您不应该创建一个新的 WaveOut 和 BufferedWaveProvider。而是创建一个,然后在收到音频时将其添加到 BufferedWaveProvider 中。

于 2013-11-06T10:40:00.540 回答
0

解决了!

我声明了一个布尔变量,然后我像这样编辑了 play_packet 函数:

dim initialized as boolean
'in the form load i set the variable to false
'and then here is the function

Private Sub play_packet(ByVal DR() As Byte)
        Dim MS As New MemoryStream(DR)
        Dim dec() As Byte = MS.ToArray
        m_bwp.AddSamples(dec, 0, dec.Length)
        MS.Close()
        MS.Dispose()
        Dim latency As Integer = 50
        Dim cbi As Wave.WaveCallbackInfo = Wave.WaveCallbackInfo.NewWindow
        Dim out_dev As New Wave.DirectSoundOut
        out_dev.Volume = 1
        If initialized = False Then
            out_dev.Init(m_bwp)
            out_dev.Play()
            initialized = True
        End If
    End Sub
于 2013-11-07T09:46:52.303 回答