Search code examples
vb.netnaudio

Pause recording using NAudio


I am working on simple Audio Recorder using NAudio and Visual Basic. I'm using .NET 4.0 and Visual Studio 2013 Community. Is it possible to pause audio recoding and then resume the recording?

This is my code to record audio :

Dim micWaveIn As IWaveIn
Dim micFileWriter As WaveFileWriter
Dim tempFolderPath As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "recordings")

Private Sub cmdStartRecord_Click(sender As Object, e As EventArgs) Handles cmdStartRecord.Click
    Dim micDevice = DirectCast(cboInputDevice.SelectedItem, MMDevice)
    micDevice.AudioEndpointVolume.Mute = False
    micWaveIn = New WasapiCapture(micDevice)

    AddHandler micWaveIn.DataAvailable, AddressOf MIC_AudioDataAvailable
    AddHandler micWaveIn.RecordingStopped, AddressOf MIC_AudioStopRecording

    Dim filePath As String = IO.Path.Combine(tempFolderPath, "mic_record.wav")
    micFileWriter = New WaveFileWriter(filePath, micWaveIn.WaveFormat)

    micWaveIn.StartRecording()
End Sub

Private Sub cmdStopRecord_Click(sender As Object, e As EventArgs) Handles cmdStopRecord.Click
    If micWaveIn IsNot Nothing Then
        micWaveIn.StopRecording()

        RemoveHandler micWaveIn.DataAvailable, AddressOf MIC_AudioDataAvailable
        RemoveHandler micWaveIn.RecordingStopped, AddressOf MIC_AudioStopRecording

        micWaveIn.Dispose()
        micWaveIn = Nothing
    End If
    If micFileWriter IsNot Nothing Then
        micFileWriter.Dispose()
        micFileWriter = Nothing
    End If
End Sub

Private Sub MIC_AudioDataAvailable(sender As Object, e As WaveInEventArgs)
    If micFileWriter IsNot Nothing Then
        micFileWriter.Write(e.Buffer, 0, e.BytesRecorded)
    End If
End Sub

Private Sub MIC_AudioStopRecording(sender As Object, e As StoppedEventArgs)
    If e.Exception IsNot Nothing Then
        MsgBox("Error while recording.", vbExclamation, "Error.")
    End If
End Sub

I'm currently thinking on this code :

Private Sub MIC_AudioDataAvailable(sender As Object, e As WaveInEventArgs)
    If micFileWriter IsNot Nothing Then
        If isRecording Then
            micFileWriter.Write(e.Buffer, 0, e.BytesRecorded)
        End If
    End If
End Sub

But the output audio can't be played in my PC. Any suggestion?


Solution

  • Yes You can Pause and resume recoding by this code i used this code in C# but you cand convert it into VB

            void pauserecording()
            {
                if (ispaused == false)
                {
                    _wavein.DataAvailable -= OndataAvailable;
                    ispaused = true;
                }
            }
            void resumerecording()
            {
                if (ispaused == true)
                {
                    _wavein.DataAvailable += OndataAvailable;
                    ispaused = false;
                }
            }