I use NAudio WaveInEvent in a telecommunications software VB.net very simple.
Sometimes, the class does not generate sound!
Would anyone have a solution?
here is my code:
Imports NAudio.Wave
Friend Class NaudioMicro
'Nombre paquets audio reçus par PTT
Private NbPaquetsAudioParPTT As Integer = 0
'Microphone Availability (if NbPaquetsAudioParPTT > 0)
Private _MicrophoneAvailability As Boolean = True
'Indique que le recorder est en mode capture
Private _IsCapturing As Boolean = False
'Indicator for Display
Private DisplaySignal As Boolean = False
'Recorder Naudio
Private WithEvents _WaveInDevice As NAudio.Wave.WaveInEvent
Friend Event OnDisplayMessage(ByVal Message As String)
Friend Event OnCaptured(ByVal AudioStream() As Byte, ByVal NumSamples As Integer)
Friend Event OnError(ByVal Source As String, ByVal Méthode As String, ByVal Message As String, ByVal Stack As String)
Friend Sub StartCapture()
Try
If _IsCapturing = False Then
_IsCapturing = True
_WaveInDevice = New WaveInEvent()
_WaveInDevice.DeviceNumber = 0
_WaveInDevice.WaveFormat = New WaveFormat(8000, 16, 1)
_WaveInDevice.NumberOfBuffers = 2
_WaveInDevice.BufferMilliseconds = 60
_WaveInDevice.StartRecording()
NbPaquetsAudioParPTT = 0
RaiseEvent OnDisplayMessage("Microphone opening...")
End If
Catch Ex As Exception
RaiseEvent OnError("NaudioMicro", "StartCapture", Ex.Message, Ex.StackTrace)
End Try
End Sub
Friend Sub StopCapture()
Try
If _IsCapturing Then
If _WaveInDevice IsNot Nothing Then
_WaveInDevice.StopRecording()
_WaveInDevice.Dispose()
_WaveInDevice = Nothing
_MicrophoneAvailability = CBool(IIf(NbPaquetsAudioParPTT > 0, True, False))
End If
_IsCapturing = False
RaiseEvent OnDisplayMessage("Closing microphone: " & NbPaquetsAudioParPTT.ToString & " packets of 960 bytes generated!")
End If
Catch Ex As Exception
RaiseEvent OnError("NaudioMicro", "StopCapture", Ex.Message, Ex.StackTrace)
End Try
End Sub
Private Sub _WaveInDevice_DataAvailable(ByVal sender As Object, ByVal e As NAudio.Wave.WaveInEventArgs) Handles _WaveInDevice.DataAvailable
NbPaquetsAudioParPTT += 1
RaiseEvent OnCaptured(e.Buffer, NbPaquetsAudioParPTT)
End Sub
Friend ReadOnly Property IsCapturing() As Boolean
Get
IsCapturing = _IsCapturing
End Get
End Property
Friend ReadOnly Property MicrophoneAvailability() As Boolean
Get
MicrophoneAvailability = _MicrophoneAvailability
End Get
End Property
End Class
Sometimes, the class does not generate sound!
Would anyone have a solution?
here is my code:
Imports NAudio.Wave
Friend Class NaudioMicro
'Nombre paquets audio reçus par PTT
Private NbPaquetsAudioParPTT As Integer = 0
'Microphone Availability (if NbPaquetsAudioParPTT > 0)
Private _MicrophoneAvailability As Boolean = True
'Indique que le recorder est en mode capture
Private _IsCapturing As Boolean = False
'Indicator for Display
Private DisplaySignal As Boolean = False
'Recorder Naudio
Private WithEvents _WaveInDevice As NAudio.Wave.WaveInEvent
Friend Event OnDisplayMessage(ByVal Message As String)
Friend Event OnCaptured(ByVal AudioStream() As Byte, ByVal NumSamples As Integer)
Friend Event OnError(ByVal Source As String, ByVal Méthode As String, ByVal Message As String, ByVal Stack As String)
Friend Sub StartCapture()
Try
If _IsCapturing = False Then
_IsCapturing = True
_WaveInDevice = New WaveInEvent()
_WaveInDevice.DeviceNumber = 0
_WaveInDevice.WaveFormat = New WaveFormat(8000, 16, 1)
_WaveInDevice.NumberOfBuffers = 2
_WaveInDevice.BufferMilliseconds = 60
_WaveInDevice.StartRecording()
NbPaquetsAudioParPTT = 0
RaiseEvent OnDisplayMessage("Microphone opening...")
End If
Catch Ex As Exception
RaiseEvent OnError("NaudioMicro", "StartCapture", Ex.Message, Ex.StackTrace)
End Try
End Sub
Friend Sub StopCapture()
Try
If _IsCapturing Then
If _WaveInDevice IsNot Nothing Then
_WaveInDevice.StopRecording()
_WaveInDevice.Dispose()
_WaveInDevice = Nothing
_MicrophoneAvailability = CBool(IIf(NbPaquetsAudioParPTT > 0, True, False))
End If
_IsCapturing = False
RaiseEvent OnDisplayMessage("Closing microphone: " & NbPaquetsAudioParPTT.ToString & " packets of 960 bytes generated!")
End If
Catch Ex As Exception
RaiseEvent OnError("NaudioMicro", "StopCapture", Ex.Message, Ex.StackTrace)
End Try
End Sub
Private Sub _WaveInDevice_DataAvailable(ByVal sender As Object, ByVal e As NAudio.Wave.WaveInEventArgs) Handles _WaveInDevice.DataAvailable
NbPaquetsAudioParPTT += 1
RaiseEvent OnCaptured(e.Buffer, NbPaquetsAudioParPTT)
End Sub
Friend ReadOnly Property IsCapturing() As Boolean
Get
IsCapturing = _IsCapturing
End Get
End Property
Friend ReadOnly Property MicrophoneAvailability() As Boolean
Get
MicrophoneAvailability = _MicrophoneAvailability
End Get
End Property
End Class