为什么录制音频会在5秒后出现错误缓冲区已满?

时间:2015-04-27 08:02:23

标签: c# naudio

以下代码给出了错误' 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();
        }

以下是stacktrace

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

1 个答案:

答案 0 :(得分:2)

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

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