1

I use NAudio to record datas from microphone, then i need to playback audio without writing a wav file yet.

Play / Pause / Stop work well, but how can I set back the position to the beginning of bwp and play back from start the audio.

I can't write a wav file yet, because I need to play back the file, navigate throught it with a slider, then erase the end of buffer with new recorded datas, then save the modified file.

private void btn_Start_Click(object sender, EventArgs e)
{
    if (sourceList.SelectedItems.Count == 0) 
        return;
    int deviceNumber = sourceList.SelectedItems[0].Index;

    wo = new WaveOutEvent();
    wi = new WaveIn();

    wi.DeviceNumber = deviceNumber;
    wi.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(deviceNumber).Channels);
    wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);

    bwp = new BufferedWaveProvider(wi.WaveFormat);
    bwp.BufferDuration = new TimeSpan(1, 0, 0);
    bwp.DiscardOnBufferOverflow = false;

    wi.StartRecording();
}

private void wi_DataAvailable(object sender, WaveInEventArgs e)
{
    bwp.AddSamples(e.Buffer, 0, e.BytesRecorded);
}

private void btn_Stop_Click(object sender, EventArgs e)
{
    wi.StopRecording();
    wo.Init(bwp);
}

private void btn_InitWaveOut_Click(object sender, EventArgs e)
{
    wo.Play();
}

private void btn_StopWaveOut_Click(object sender, EventArgs e)
{
    wo.Stop();
}

private void btn_PauseWaveOut_Click(object sender, EventArgs e)
{
    wo.Pause();
}
slugster
  • 49,403
  • 14
  • 95
  • 145

1 Answers1

0

The BufferedWaveProvider is not designed to support repositioning. If you want that you should make your own IWaveProvider derived class that holds onto all bytes received to allow repositioning. Obviously you'd want to be careful about how much memory you use up as audio data can grow quite large over time.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Thanks for your response Mark (and your library btw!) So do you think it's a better idea to work with wav files instead of memory ? Record > save to wav file > play this wav file > trim it, create a new wave file with the second record and then concatenate the two files ? etc etc. –  May 08 '18 at 18:16