定义音频捕捉设备(SoundRecord类)
public const int cNotifyNum = 16; // 缓冲队列的数目
private int mNextCaptureOffset = 0; // 该次录音缓冲区的起始点
private int mSampleCount = 0; // 录制的样本数目
private int mNotifySize = 0; // 每次通知大小
private int mBufferSize = 0; // 缓冲队列大小
private string mFileName = string.Empty; // 文件名
private FileStream mWaveFile = null; // 文件流
private BinaryWriter mWriter = null; // 写文件
private Capture mCapDev = null; // 音频捕捉设备
private CaptureBuffer mRecBuffer = null; // 缓冲区对象
private Notify mNotify = null; // 消息通知对象
private WaveFormat mWavFormat; // 录音的格式
private Thread mNotifyThread = null; // 处理缓冲区消息的线程
private AutoResetEvent mNotificationEvent = null; // 通知事件
public SoundRecord()
{
InitializeComponent();
// 初始化音频捕捉设备
InitCaptureDevice();
// 设定录音格式
mWavFormat = CreateWaveFormat();
}
///
/// 设定录音结束后保存的文件,包括路径
///
/// 保存wav文件的路径名
public void SetFileName(string filename)
{
mFileName = filename;
}
///
/// 开始录音
///
public void RecStart()
{
// 创建录音文件
CreateSoundFile();
// 创建一个录音缓冲区,并开始录音
CreateCaptureBuffer();
// 建立通知消息,当缓冲区满的时候处理方法
InitNotifications();
mRecBuffer.Start(true);
}
///
/// 停止录音
///
public void RecStop()
{
// 关闭通知消息
if (null != mNotificationEvent)
mNotificationEvent.Set();
// 停止录音
mRecBuffer.Stop();
// 写入缓冲区最后的数据
RecordCapturedData();
// 回写长度信息
mWriter.Seek(4, SeekOrigin.Begin