Replay a BufferedWaveProvider

597 views Asked by At

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();
}
1

There are 1 answers

1
Mark Heath On

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.