使用NAudio录制音频使我无法接听

问题描述 投票:0回答:1

我正在录制音频,然后将其发送到Google语音,然后发送给文本,但是当我使用naudio进行音频制作时,它只会录制5秒钟,然后从那里停止录制。我将代码复制到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;
        }
    }
}
c# winforms naudio google-speech-to-text-api
1个回答
1
投票

您获得5秒钟的音频剪辑不仅是因为您从BufferLength读取了一次BufferedWaveProvider ...

int count = bwp.BufferLength;

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

... [5秒是default value of that property,但是因为BufferedWaveProvider uses a circular buffer,所以BufferLength是它所有可用的数据。

对我有用的是跳过BufferedWaveProvider,并在可用时尽快将新数据写入WaveFileWriter ...

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

为了支持该更改,按钮事件处理程序将更改为以下内容...

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

这似乎与Recording a WAV file in a WinForms app with WaveIn中使用的方法相同。

© www.soinside.com 2019 - 2024. All rights reserved.