C# mci SoundRecord / 录音
发布时间:2020-12-15 17:53:20 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 private SoundRecord m_Record = new SoundRecord(); private void MainForm_Load(object sender,EventArgs e) { this.m_Record.OpenRecord(); // 打
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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:UserswindoDesktop1.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) != 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(' ',12); if (mciSendString("status movie mode",strBuffer,12,0) != ERROR_SUCCESS) return MODE_UNKNOWN; if ((strBuffer = strBuffer.Remove(strBuffer.IndexOf(' '))).Length <= 0) return MODE_UNKNOWN; return strBuffer; } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |