Hi again,
I have slightly refactored my code based on your recommendations, using now v 1.7 - message displayed in VS was there because of targeting my app to .NET 2. I retargeted it to .NET 4 and now it's OK.
The worst thing: The issue persists - now it happenes when about 2MBytes are transferred, but number is not same in all cases. One side stops playing received sound... But when I think, the issue can be that one side stops recording,but still sending data. Is that possible? Any suggestions?
Of course, I'll continue to inspect, what's wrong and will let you know, if I find something.
I have slightly refactored my code based on your recommendations, using now v 1.7 - message displayed in VS was there because of targeting my app to .NET 2. I retargeted it to .NET 4 and now it's OK.
The worst thing: The issue persists - now it happenes when about 2MBytes are transferred, but number is not same in all cases. One side stops playing received sound... But when I think, the issue can be that one side stops recording,but still sending data. Is that possible? Any suggestions?
Of course, I'll continue to inspect, what's wrong and will let you know, if I find something.
Imports NAudio
Imports System.IO
Public Class SoundHelper
Private WithEvents wav As New Wave.WaveIn()
Public Event AudioDataToSend(ByVal AudioBytes() As Byte)
Private m_bwp As New Wave.BufferedWaveProvider(New Wave.WaveFormat(8000, 16, 1))
Private out_dev As New Wave.DirectSoundOut
Private initialized As Boolean = False
Public Sub New()
SetDefaultBufferConfiguration()
End Sub
Public Sub New(ByVal blnStartRecording As Boolean)
SetDefaultBufferConfiguration()
If blnStartRecording Then
StartRecording()
End If
End Sub
Private Sub SetDefaultBufferConfiguration()
m_bwp.BufferLength = 80000
m_bwp.DiscardOnBufferOverflow = True
End Sub
Public Sub StartRecording()
wav.StartRecording()
End Sub
Public Sub StopRecording()
wav.StopRecording()
End Sub
Private Sub wav_DataAvailable(ByVal sender As Object, ByVal e As NAudio.Wave.WaveInEventArgs) Handles wav.DataAvailable
RaiseEvent AudioDataToSend(e.Buffer)
End Sub
Public Sub PlayData(ByVal AudioBytes() As Byte)
Dim MS As New MemoryStream(AudioBytes)
Dim dec() As Byte = MS.ToArray
m_bwp.AddSamples(dec, 0, dec.Length)
MS.Close()
Dim latency As Integer = 50
Dim cbi As Wave.WaveCallbackInfo = Wave.WaveCallbackInfo.FunctionCallback
out_dev.Volume = 1
If initialized = False Then
out_dev.Init(m_bwp)
out_dev.Play()
initialized = True
End If
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
End Class