正如标题所说,我正在尝试使用vb.net 2010和NAudio库实时存储和播放麦克风数据。我从NAudio Codeplex网站获得了C#中的代码,并将其翻译成vb.net。 代码中没有错误,程序也可以正常工作(每次有麦克风数据可用时,我都会设置一个不断增加和更新标签的计数器,看看它是否真的有效),但我听不到任何声音。
这是代码
Imports NAudio Imports System.IO
'Libraries I'm usingPrivate Sub wi_dataAvailable(ByVal obj As Object, ByVal e As Wave.WaveInEventArgs) Handles wi.DataAvailable count += 1 'here is where the counter increases Label1.Text = count 'and here the label is updated and it seems to work fine play_packet(e.Buffer) End Sub
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
希望你能帮助我!
答案 0 :(得分:0)
每次收到录制的缓冲区时,都不应该创建新的WaveOut和BufferedWaveProvider。而是创建其中一个,并在收到时将音频添加到BufferedWaveProvider中。
答案 1 :(得分: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