Hello,
I use NAudio for professional project to communicate with radios. Randomly, the DLL crashes at start of the Micro capture.
Someone would have an idea?
Thank you in advance.
Here is my code:
Imports NAudio.Wave
Imports System.Runtime.InteropServices
Public Class NaudioMicro
Implements IDisposable
Protected Disposed As Boolean = False
#Region "Déclarations"
'Autorisation d'exporter le flux Audio
Private Transmit As Integer = 0
'Indique que le recorder est en mode capture
Private IsCapturing As Integer = 0
'Recorder Naudio
Private WithEvents _WaveInDevice As WaveIn
#End Region
#Region "Constructor"
''' <summary>
''' Simple New without Data
''' </summary>
''' <remarks>The selection of Peiker Microphone is automatic !</remarks>
Public Sub New()
Dim DeviceInfoI As WaveInCapabilities
Try
'1. Détection automatique du Micro Peiker
Dim DeviceNumber As Integer = -1
For I As Integer = 0 To WaveIn.DeviceCount - 1
DeviceInfoI = WaveIn.GetCapabilities(I)
If Val("&H" & Strings.Left(DeviceInfoI.ProductGuid.ToString, 2)) > 0 Then
If InStr(DeviceInfoI.ProductName, "PTC USB", CompareMethod.Text) > 0 Then
DeviceNumber = I
Exit For
End If
End If
Next
'2. Configuration du Recorder avec Micro Peiker
If DeviceNumber >= 0 Then
_WaveInDevice = New WaveIn()
_WaveInDevice.DeviceNumber = DeviceNumber
_WaveInDevice.WaveFormat = New NAudio.Wave.WaveFormat(8000, 16, 1)
_WaveInDevice.BufferMilliseconds = 60
End If
Catch Ex As Exception
Finally
IsCapturing = 0
End Try
End Sub
#End Region
#Region "Event"
Friend Event OnDeliverySpeechData(ByVal SpeechData() As Byte)
#End Region
#Region "Commandes"
''' <summary>
''' Start Recording
''' </summary>
''' <remarks></remarks>
Private Sub CaptureStart()
If _WaveInDevice Is Nothing Then
Return
End If
If IsCapturing = 0 Then
IsCapturing = 1
_WaveInDevice.StartRecording()
End If
End Sub
''' <summary>
''' Stop Recording
''' </summary>
''' <remarks></remarks>
Private Sub CaptureStop()
If _WaveInDevice Is Nothing Then
Return
End If
If IsCapturing = 1 Then
IsCapturing = 0
_WaveInDevice.StopRecording()
Transmit = 0
End If
End Sub
''' <summary>
''' Start transmission of audio stream
''' </summary>
''' <remarks></remarks>
Friend Sub StartCapture()
Transmit = 1
If IsCapturing = 0 Then
CaptureStart()
End If
End Sub
''' <summary>
''' Stop transmission of audio stream
''' </summary>
''' <remarks></remarks>
Friend Sub StopCapture()
Transmit = 0
If IsCapturing = 1 Then
CaptureStop()
End If
End Sub
#End Region
#Region "Processus_internes"
''' <summary>
''' Fonction CallBack de l'enregistreur
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub _WaveInDevice_DataAvailable(ByVal sender As Object, ByVal e As NAudio.Wave.WaveInEventArgs) Handles _WaveInDevice.DataAvailable
If Transmit = 1 Then
If e.BytesRecorded > 0 Then
If My.Settings.SendSpeechDirect = 1 Then
Dim AIOPCMFrame As New _D25AIOPCMFRAME
AIOPCMFrame.sz = e.Buffer.Length
AIOPCMFrame.m_aFrame = Marshal.AllocHGlobal(e.Buffer.Length)
Marshal.Copy(e.Buffer, 0, AIOPCMFrame.m_aFrame, e.Buffer.Length)
Call Appels.DiffusionMicro(AIOPCMFrame)
If LoopBack = 1 Then
If IHM.PlayBackJingleInProgress = 0 Then
If My.Settings.AudioEmbeded = 0 Then
Players.Player(0).PlayPacket(e.Buffer)
Else
LocalPlayer.PlayPacket(e.Buffer)
End If
End If
End If
Else
RaiseEvent OnDeliverySpeechData(e.Buffer)
End If
End If
End If
End Sub
#End Region
#Region "IDisposable Support"
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If Not Me.Disposed Then
If disposing Then
If _WaveInDevice IsNot Nothing Then
_WaveInDevice.StopRecording()
_WaveInDevice.Dispose()
_WaveInDevice = Nothing
End If
End If
End If
Me.Disposed = True
End Sub
' Do not change or add Overridable to thse methods.
' Put cleanup code in Dispose(Byval disposing as boolean).
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
End Class
Comments: sounds like an out of memory issue