简体   繁体   English

使用 NAudio 录制音频让我感到厌烦

[英]Recording audio with NAudio cuts me off

I am recording an audio to send it to send it to Google speech to text but when I make the audio with naudio it records me only 5 seconds and from there it stops recording.我正在录制音频以将其发送到谷歌语音文本,但是当我使用 naudio 制作音频时,它只记录了我 5 秒,然后它停止记录。 I copy the code in C #, this is my first time using this API, but I don't know why it cuts me, if it should stop recording when I press the save button, the application is a simple form with 2 buttons, one for recording and the other for stop.我在C#中复制代码,这是我第一次使用这个API,但我不知道为什么它会削减我,如果我按下保存按钮时它应该停止录制,该应用程序是一个带有2个按钮的简单表单,一个用于记录,另一个用于停止。

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private BufferedWaveProvider bwp;
        WaveIn waveIn;
        WaveOut waveOut;
        WaveFileWriter writer;
        WaveFileReader reader;
        string output = "audio.raw";
        public Form1()
        {
            InitializeComponent();
            waveOut = new WaveOut();
            waveIn = new WaveIn();

            waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
            waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
            bwp = new BufferedWaveProvider(waveIn.WaveFormat);
            bwp.DiscardOnBufferOverflow = true;

            btnRecordVoice.Enabled = true;
            btnSave.Enabled = false;
            //btnSpeechInfo.Enabled = false;
        }

        private void btnRecordVoice_Click(object sender, EventArgs e)
        {
            if (NAudio.Wave.WaveIn.DeviceCount < 1)
            {
                Console.WriteLine("No se encuentra un microfono!");
                return;
            }

            waveIn.StartRecording();

            btnRecordVoice.Enabled = false;
            btnSave.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            waveIn.StopRecording();

            if (File.Exists("audio.raw"))
                File.Delete("audio.raw");

            writer = new WaveFileWriter(output, waveIn.WaveFormat);

            btnRecordVoice.Enabled = false;
            btnSave.Enabled = false;

            byte[] buffer = new byte[bwp.BufferLength];
            int offset = 0;
            int count = bwp.BufferLength;

            var read = bwp.Read(buffer, offset, count);
            if (count > 0)
            {
                writer.Write(buffer, offset, read);
            }

            waveIn.Dispose();
            waveIn = null;
            writer.Close();
            writer = null;

            reader = new WaveFileReader("audio.raw"); // (new MemoryStream(bytes));
            waveOut.Init(reader);
            waveOut.PlaybackStopped += new EventHandler<StoppedEventArgs>(waveOut_PlaybackStopped);
            waveOut.Play();
        }

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

        }

        private void waveOut_PlaybackStopped(object sender, StoppedEventArgs e)
        {
            waveOut.Stop();
            reader.Close();
            reader = null;
        }
    }
}

You get a 5-second audio clip not only because you do a one-time BufferLength read from the BufferedWaveProvider ...您获得 5 秒的音频剪辑不仅是因为您从BufferedWaveProvider执行了一次性的BufferLength读取...

int count = bwp.BufferLength;

var read = bwp.Read(buffer, offset, count);

...and 5 seconds is the default value of that property , but because BufferedWaveProvider uses a circular buffer , so BufferLength is all the data it has available. ...并且 5 秒是该属性默认值,但因为BufferedWaveProvider 使用循环缓冲区,所以BufferLength是它拥有的所有数据。

What worked for me was to skip the BufferedWaveProvider and write new data to the WaveFileWriter as soon as it becomes available...对我WaveFileWriter是跳过BufferedWaveProvider并在新数据可用时立即将新数据写入WaveFileWriter ...

void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
    writer.Write(e.Buffer, 0, e.BytesRecorded);
}

To support that change, the button event handlers get changed to the following...为了支持这种更改,按钮事件处理程序更改为以下内容...

private void btnRecordVoice_Click(object sender, EventArgs e)
{
    if (NAudio.Wave.WaveIn.DeviceCount < 1)
    {
        Console.WriteLine("No se encuentra un microfono!");
        return;
    }

    writer = new WaveFileWriter(output, waveIn.WaveFormat);
    waveIn.StartRecording();

    btnRecordVoice.Enabled = false;
    btnSave.Enabled = true;
}

private void btnSave_Click(object sender, EventArgs e)
{
    waveIn.StopRecording();
    writer.Close();
    writer = null;

    btnRecordVoice.Enabled = false;
    btnSave.Enabled = false;

    reader = new WaveFileReader("audio.raw"); // (new MemoryStream(bytes));
    waveOut.Init(reader);
    waveOut.PlaybackStopped += new EventHandler<StoppedEventArgs>(waveOut_PlaybackStopped);
    waveOut.Play();
}

This appears to be the same approach used in Recording a WAV file in a WinForms app with WaveIn .这似乎与使用 WaveIn 在 WinForms 应用程序记录 WAV 文件中使用的方法相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM