2011-04-17 198 views

回答

4

最好的方法是使用BufferedWaveProvider作爲WaveOut的輸入。然後在WaveIn的DataAvailable回調,供應記錄到BufferedWaveProvider

void DataAvailable(object sender, WaveInEventArgs args) 
{ 
    bufferedWaveProvider.AddSamples(args.Buffer, 0, args.BytesRecorded); 
} 

您需要注意的是,默認的緩衝區大小會導致明顯的延遲,因此,如果您希望您可能需要低延遲數據嘗試一下緩衝區大小,看看你有多低可以得到它。

12

這裏是爲我工作的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using NAudio.Wave; 
using NAudio.CoreAudioApi; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form4 : Form 
    { 

     private BufferedWaveProvider bwp; 

     WaveIn wi; 
     WaveOut wo; 
     public Form4() 
     { 
      InitializeComponent(); 
      wo = new WaveOut(); 
      wi = new WaveIn(); 

      wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable); 

      bwp = new BufferedWaveProvider(wi.WaveFormat); 
      bwp.DiscardOnBufferOverflow = true; 

      wo.Init(bwp); 
      wi.StartRecording(); 
      wo.Play(); 

     } 

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

     } 
    } 
}