#region 公開方法
public void Stop()
{
isRunning = false;
if (m_Buffer != null) m_Buffer.Stop();
}
public void Write(byte[] data)
{
try
{
Console.WriteLine("播放聲音:{0}", data.Length);
circularBuffer.Write(data);
}
catch (Exception) { }
}
#endregion
public class CaptureSound
{
#region 私有成員
private const int NumberRecordNotifications = 4;
private readonly BufferPositionNotify[] positionNotify;
private Capture selectedDevice;
private CaptureBuffer buffer;
private int captureBufferSize;
private string fileName;
private bool isRecording;
private int nextCaptureOffset;
private AutoResetEvent notificationEvent;
private Notify notify;
private int notifySize;
private Thread notifyThread;
private long sampleCount;
private WaveFormat selectedFormat;
private FileStream waveFile;
private BinaryWriter writer;
#endregion
// ...
}
關鍵點:
• circularBuffer 環狀緩衝區解耦網路接收與播放執行緒。
• NumberRecordNotifications = 4 將緩衝區切四段,觸發通知頻率平衡延遲與 CPU 佔用。