用NAudio录制时如何显示录制时间?

时间:2015-07-10 12:29:15

标签: c# timer naudio

我正在尝试用NAudio Library使用C#windows窗体Timer进行录制时显示录制时间。这是我想要实现的代码。我对此代码的问题是定时器的时序与录音长度。我希望定时器的定时和录音时间同步!

  private void buttonStart_Click(object sender, EventArgs e)
    {

        buttonStart.Enabled = false;
        buttonStop.Enabled = true;

        waveSource = new WaveIn();
        waveSource.WaveFormat = new WaveFormat(44100, 2);

        waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
        waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);

        waveFile = new WaveFileWriter(@"C:\one\Test0010.wav", waveSource.WaveFormat);
        waveSource.StartRecording();

      } 
  void waveSource_DataAvailable(object sender, WaveInEventArgs e)
    {

        if (waveFile != null)
        {

            timerSoundRecord.Start();
            waveFile.Write(e.Buffer, 0, e.BytesRecorded);
            waveFile.Flush();
            var lenght = (int)(waveFile.Length / waveFile.WaveFormat.AverageBytesPerSecond);

            if (lenght == 6)
            {
                timerSoundRecord.Stop();
                waveSource.StopRecording();
                buttonStop.Enabled = false;
                buttonStart.Enabled = true;
            }



        }
    }

private void timerSoundRecord_Tick(object sender, EventArgs e)
    {

        if (progressBarRecordSound.Value != 6)
        {


            seconds = seconds + 1;

            labelTime.Text = @"00:0" + seconds;
            progressBarRecordSound.Value++;
        }
        else
        {
            timerSoundRecord.Stop();
        }
    }

1 个答案:

答案 0 :(得分:1)

您在每次滴答时更新了timerSoundRecord_Tick内的秒数。这些是非常不同的测量。

只需从方法waveSource_DataAvailable global中设置长度变量,然后在timerSoundRecord_Tick中检查它,而不是在你增加的时间内使用该变量。

  private void buttonStart_Click(object sender, EventArgs e)
{

    buttonStart.Enabled = false;
    buttonStop.Enabled = true;

    waveSource = new WaveIn();
    waveSource.WaveFormat = new WaveFormat(44100, 2);

    waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
    waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);

    waveFile = new WaveFileWriter(@"C:\one\Test0010.wav", waveSource.WaveFormat);
    waveSource.StartRecording();
    timerSoundRecord.Start();
  } 

int length = 0;
void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
    if (waveFile != null)
    {           
        waveFile.Write(e.Buffer, 0, e.BytesRecorded);
        waveFile.Flush();
        var lenght = (int)(waveFile.Length / waveFile.WaveFormat.AverageBytesPerSecond);

        if (lenght == 6)
        {
            timerSoundRecord.Stop();
            waveSource.StopRecording();
            labelTime.Text = @"00:0" + length;
            progressBarRecordSound.Value++;
            buttonStop.Enabled = false;
            buttonStart.Enabled = true;
        }
    }
}

private void timerSoundRecord_Tick(object sender, EventArgs e)
{
        labelTime.Text = @"00:0" + length;
        progressBarRecordSound.Value++;
}