2
votes

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?

2

2 Answers

2
votes

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;
            }
        }
0
votes

"StopRecording" and then start it again worked better for me (I'm using .net 2.0 and my NAudio version is older than you).

In my case, I wanted to record several videos and in parallel, record a single audio.Then concatenating videos in a single file and merge it with audio file.
Audio and video wasn't sync. Audio was longer than video (video came first and then audio was comming)

After that I used the trick that mentioned. Means when a video capture stopped and new video capture getting ready to start, I paused audio recording with

yourWaveIn.DataAvailable -= OndataAvailable;


Again audio and video wasn't syn, but in a different way. Audio was much shorter than video (audio came first and then video).

When I used "stopping" instead, result was great.

I don't know why but it seems disconnecting "OndataAvailable()" from it's eventHandler caused loosing some packages (that caused shorter audio file) and "StopRecording" doesn't have that issue.

Hope my experience being useful to someone.