我目前正尝试通过WasapiCapture和WaveWriter(CSCore包)录制音频,并希望在用户停止讲话后停止录制。谁能帮我吗?
我想到的是,我可以创建一个RecordingVolumeHandler,当Volume低于某个阈值1或2秒时触发。我怎样才能做到这一点?有没有一种方法可以从当前正在录制的音频输入中获取音量?
以下代码用于启动录制和停止录制的两个功能。
private WasapiCapture capture;
private WaveWriter writer;
private void startRecording()
{
capture = new WasapiCapture();
capture.Initialize();
writer = new WaveWriter("file.wav", capture.WaveFormat);
capture.DataAvailable += (s, capData) =>
{
writer.Write(capData.Data, capData.Offset, capData.ByteCount);
};
capture.Start();
}
private void stopRecording()
{
if (writer != null && capture != null)
{
capture.Stop();
writer.Dispose();
capture.Dispose();
}
}
提前谢谢!
答案 0 :(得分:0)
您可以添加Timer
计数静默期。到期时 - 停止录制。我还没有测试过这段代码。定时器可能无法重置,但想法应该清楚。
private WasapiCapture capture;
private WaveWriter writer;
private Timer silenceTimer;
public Constructor()
{
silenceTimer = new Timer();
silenceTimer.Interval = 5000; // 5 seconds
silenceTimer.Elapsed +=SilenceTimerElapsed;
}
private void SilenceTimerElapsed(object sender, ElapsedEventArgs e)
{
silenceTimer.Stop();
stopRecording();
}
private void startRecording()
{
capture = new WasapiCapture();
capture.Initialize();
writer = new WaveWriter("file.wav", capture.WaveFormat);
capture.DataAvailable += (s, capData) =>
{
writer.Write(capData.Data, capData.Offset, capData.ByteCount);
silenceTimer.Stop();
silenceTimer.Start(); // Resetting timer
};
silenceTimer.Start();
capture.Start();
}
private void stopRecording()
{
if (writer != null && capture != null)
{
capture.Stop();
writer.Dispose();
capture.Dispose();
}
}