2017-07-21 7 views
2

現在、WasapiCaptureとWaveWriter(CSCoreパッケージ)経由でオーディオを録音しようとしています。誰か助けてくれますか?C#CSCoreは、ユーザーが話を停止したときに録音を停止します。

私の考えでは、Volumeが1または2秒間一定のしきい値を下回ったときにトリガーするRecordingVolumeHandlerを作成する可能性があるということです。これどうやってするの?現在録音されているオーディオ入力からボリュームを取得する方法はありますか?

次のコードは、録音を開始して停止するための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

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(); 
    } 
} 
関連する問題