清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
录音我一般更喜欢mci方式,当然也有另一种方式及DirectSound不过它会麻烦许多
但是它却有一个让我感到较好的特点,首先可以选择录音设备源,第二它不像mci
必须要写到本地磁盘,它是把录制音频流存放在内存中,在速度上我认为它会快很
多,不过它也有一个缺点让我不舒服,它不如mci哪样是默认集成在系统中的,我这
个人特喜欢使用系统自带的函数,主要你不需要去拷贝DLL因为系统是默认就有的,
只需要去调用就行了,方便 当然mci提供的操作命令我比较讨厌mciSendCommand
原因在于我自己得写一大堆结构体 是让我感到非常讨厌的,mciSendString则是通过
字符串,属于轻量操作函数我倒是喜欢的紧。
private SoundRecord m_Record = new SoundRecord(); private void MainForm_Load(object sender, EventArgs e) { this.m_Record.OpenRecord(); // 打开录音 this.m_Record.StartRecord(); // 开始录音 } private void BtnStopAndSave_Click(object sender, EventArgs e) { this.m_Record.StopRecord(); // 停止录音 this.m_Record.SaveRecord(@"C:\Users\windo\Desktop\1.wav"); // 保存录音 this.m_Record.CloseRecord(); // 关闭录音 }上面的代码只是一个简单的录音并保存的过程,不过该类本身就很简单 而且我也附
有注释,想必你们应该不会感到学习困难,录音属性配置默认初始化在构造函数中。
public partial class SoundRecord { [DllImport("WinMm.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback); private const int ERROR_SUCCESS = 0; private const string MODE_UNKNOWN = "unknown"; private static bool mciSendString(string strCommand) { return mciSendString(strCommand, null, 0, 0) != ERROR_SUCCESS; } } public partial class SoundRecord { private int m_channels; private int m_sample_spersec; private string m_format_tag; private int m_bits_per_sample; public SoundRecord() { this.Channels = 2; this.FormatTag = "pcm"; this.BitsPerSample = 8; this.SampleSpersec = 11025; } // 采样位数 public virtual int BitsPerSample { set { if (mciSendString("set wave bitpersample " + value)) this.m_bits_per_sample = value; } get { return this.m_bits_per_sample; } } // 采样频率 public virtual int SampleSpersec { get { return this.m_sample_spersec; } set { if (mciSendString("set wave samplespersec " + value)) this.m_sample_spersec = value; } } // 声道 public virtual int Channels { get { return m_channels; } set { if (mciSendString("set wave channels " + value)) this.m_channels = value; } } // 格式标签 public virtual string FormatTag { get { return this.m_format_tag; } set { if (mciSendString("set wave format tag " + value)) this.m_format_tag = value; } } // 打开录音 public virtual bool OpenRecord() { return mciSendString("open new type waveaudio alias movie"); } // 开始录音 public virtual bool StartRecord() { return mciSendString("record movie"); } // 停止录音 public virtual bool StopRecord() { return mciSendString("stop movie"); } // 保存录音 public virtual bool SaveRecord(string saveFileName) { return mciSendString("save movie " + saveFileName); } // 关闭录音 public virtual bool CloseRecord() { return mciSendString("close movie"); } // 暂停录音 public virtual bool PauseRecord() { return mciSendString("pause movie"); } // 恢复录音 public virtual bool ResumeRecord() { return mciSendString("resume movie"); } // 执行状态 public virtual string Status { get { string strBuffer = new string('\0', 12); if (mciSendString("status movie mode", strBuffer, 12, 0) != ERROR_SUCCESS) return MODE_UNKNOWN; if ((strBuffer = strBuffer.Remove(strBuffer.IndexOf('\0'))).Length <= 0) return MODE_UNKNOWN; return strBuffer; } } }