为什么录音 5 秒后报错 "buffer full"?

Why recording audio gives error "buffer full" after 5 seconds?

以下代码给出了错误 'Buffer full',我不知道为什么。我的代码应该在录制音频时给我波形图。但是,波形图正在运行,并且在我的程序

屏幕上出现上述错误 5 秒后

我是新手。

private void btnRecord_Click(object sender, EventArgs e)
        {
            if (btnRecord.Text == "Record")
            {
                sources = new List<NAudio.Wave.WaveInCapabilities>();

                for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++)
                {
                    sources.Add(NAudio.Wave.WaveIn.GetCapabilities(i));
                }

                input = new NAudio.Wave.WaveIn();
                input.DeviceNumber = 0;
                input.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(0).Channels);

                NAudio.Wave.WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(input);

                waveOut = new NAudio.Wave.DirectSoundOut();
                waveOut.Init(waveIn);

                input.StartRecording();

                //waveOut.Play();
                input.DataAvailable += input_DataAvailable;
                btnRecord.Text = "Stop Record";
            }
            else if (btnRecord.Text == "Stop Record")
            {
                waveOut.Stop();
                input.StopRecording();
                btnRecord.Text = "Record";
                input.Dispose();

            }
        }

private void input_DataAvailable(object sender, WaveInEventArgs e)
        {
            MemoryStream memStream = new MemoryStream();
            memStream.Write(e.Buffer, 0, e.BytesRecorded);            


            RawSourceWaveStream rawSource = new RawSourceWaveStream(memStream, input.WaveFormat);
            customWaveViewer2.WaveStream = rawSource;
            rawSource.Flush();
            memStream.Flush();
        }

以下是堆栈跟踪

at NAudio.Wave.BufferedWaveProvider.AddSamples(Byte[] buffer, Int32 offset, Int32 count)
   at NAudio.Wave.WaveInProvider.waveIn_DataAvailable(Object sender, WaveInEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at NAudio.Wave.WaveIn.RaiseDataAvailable(WaveInBuffer buffer)
   at NAudio.Wave.WaveIn.Callback(IntPtr waveInHandle, WaveMessage message, IntPtr userData, WaveHeader waveHeader, IntPtr reserved)
   at NAudio.Wave.WaveWindow.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Pitch_Comparer.Program.Main() in c:\Users\Nadeesha\Documents\Visual Studio 2012\Projects\Pitch_Comparer\Pitch_Comparer\Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

WaveInProvider 正在录制音频并将其放入 BufferedWaveProvider。所以您的 WaveOut 需要播放,否则缓冲区将填满。 (因此取消注释 waveOut.Play 以解决问题)。

如果您实际上不想播放正在录制的音频,那么只需使用常规 WaveIn,而不是 WaveInProvider