我正在使用NAudio和Visual Basic处理简单的Audio Recorder。我正在使用.NET 4.0和Visual Studio 2013社区。 是否可以暂停音频重新编码然后恢复录制?
这是我录制音频的代码:
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
我正在考虑这段代码:
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
但输出音频无法在我的电脑上播放。有什么建议吗?
答案 0 :(得分:1)
是的你可以通过这段代码暂停和恢复重新编码我在C#中使用了这段代码,但你可以把它转换成VB
void pauserecording()
{
if (ispaused == false)
{
_wavein.DataAvailable -= OndataAvailable;
ispaused = true;
}
}
void resumerecording()
{
if (ispaused == true)
{
_wavein.DataAvailable += OndataAvailable;
ispaused = false;
}
}
答案 1 :(得分:0)
“StopRecording”,然后再次启动它对我来说效果更好(我使用.net 2.0,我的NAudio版本比你年长)。
就我而言,我想要录制几个视频并同时录制单个音频。然后将视频连接到单个文件中并将其与音频文件合并。
音频和视频不同步。音频比视频长(视频首先出现,然后音频出现)
之后我使用了提到的技巧。意味着当视频捕获停止并且新的视频捕获准备开始时,我用
yourWaveIn.DataAvailable -= OndataAvailable;
音频和视频再次不是同义词,而是以不同的方式。音频比视频要短得多(首先是音频,然后是视频)。
当我用“停止”代替时,结果很棒。
我不知道为什么但似乎从它的eventHandler断开“OndataAvailable()”导致丢失一些包(导致更短的音频文件)和“StopRecording”没有那个问题。
希望我的经历对某人有用。