So I can't figure out why this fails when run from a timer, but executes fine through button control. Using alvas.audio to capture wave file, when detecting a "silent" period it saves the file, processes it and then the idea is to delete that file and start a new recording.
The problem is when I do through through a push button, it works fine, the file is captured, processed and clicking the button calls StartRecord() to start a new recording which enables the timer to look for the silent period but does not rely on the timer to restart the recording after processing - no issues I can do this over and over without a problem.
Having it automated through a timer control it fails, after the first recording the timer should restart the recording by calling StartRecord() again but I get an error "The specified device handle is invalid". I thought maybe it was all happening too fast so I tried sleeping in between the restart for up to a full minute with no change, I removed the ProcessSpeech() sub routine from the timer and just had it record, and sleep for a minute then restart and still fail with same error. I can do the manual button start much faster than a minute with no issue.
The problem is when I do through through a push button, it works fine, the file is captured, processed and clicking the button calls StartRecord() to start a new recording which enables the timer to look for the silent period but does not rely on the timer to restart the recording after processing - no issues I can do this over and over without a problem.
Having it automated through a timer control it fails, after the first recording the timer should restart the recording by calling StartRecord() again but I get an error "The specified device handle is invalid". I thought maybe it was all happening too fast so I tried sleeping in between the restart for up to a full minute with no change, I removed the ProcessSpeech() sub routine from the timer and just had it record, and sleep for a minute then restart and still fail with same error. I can do the manual button start much faster than a minute with no issue.
VB.NET:
[LEFT]
Private Sub StartRecord()
Dim fileName As String = "c:\test.wav"
Dim stream As Stream = Nothing
Dim arw As IAudioReadWriter = Nothing
tmrCapture.Enabled = False
If File.Exists(fileName) Then
File.Delete(fileName)
stream = File.Create(fileName)
Else
stream = File.Create(fileName)
End If
arw = New WaveReadWriter(stream, AudioCompressionManager.FormatBytes(m_format))
rp.Open(arw)
rp.Record() 'start recording - this is where it fails when being called from the timer after the first recording
End Sub
Private Sub tmrCapture_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCapture.Tick
If SilentCounter >= LastDuration Then LastDuration = SilentCounter
If SilentCounter <= LastDuration Then
TextBox1.Text = "Finished"
SilentCounter = 0
LastDuration = 0
rp.Stop() 'stop recording
rp.Close() 'close file to process
ProcessSpeech() 'process file for speech reco
StartRecord() 'Start listening again for next capture
tmrCapture.Enabled = False
End If
End Sub
[/LEFT]
Last edited: