2015-04-27 41 views
3

以下代碼給我錯誤'緩衝區滿',我不知道爲什麼。我的代碼應該在錄製音頻時給我波形圖。然而,波形圖工作和5秒以上的錯誤被帶到屏幕我的程序爲什麼錄音音頻在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() 
+0

這是您的完整代碼?我懷疑你對傳入的樣本做了一些處理。 – DrKoch

+0

像什麼?你能詳細解釋一下嗎? – gayan1991

+1

我想你在錄製過程中使用了太多的CPU,這就是爲什麼有些緩衝區顯示溢出 – DrKoch

回答

2

WaveInProvider正在錄製音頻,並把它變成一個BufferedWaveProvider。所以你的WaveOut需要播放或緩衝區將被填滿。 (所以取消註釋waveOut.Play以解決問題)。

如果您實際上不想播放您正在錄製的音頻,則只需使用常規的WaveIn而不是WaveInProvider