使用winmm.dll 获取麦克风声音数据
//录音 /// <summary> /// 初始化录音环境 /// </summary> /// <returns></returns> public static bool InitalDevice() { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" "));//11025 ilong = APIClass.mciSendString("set media bitpersample 8", durLength, durLength.Length, 0); ilong = APIClass.mciSendString("set media samplespersec 11025", durLength, durLength.Length, 0); ilong = APIClass.mciSendString("set media channels 2", durLength, durLength.Length, 0); ilong = APIClass.mciSendString("set media format tag pcm", durLength, durLength.Length, 0); return true; } /// <summary> /// 开始录音 /// </summary> /// <returns></returns> public static bool BeginRecord() { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); ilong = APIClass.mciSendString("close My", durLength, durLength.Length, 0); try { ilong = APIClass.mciSendString("open new type waveaudio alias My", durLength, durLength.Length, 0); ilong = APIClass.mciSendString("record My", durLength, durLength.Length, 0); } catch (Exception ex) { ex.Message.ToString(); } return true; } /// <summary> /// 保存录音 /// </summary> /// <param name="path">保存路径</param> public static void SaveMusic(string path) { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); try { ilong = APIClass.mciSendString("save My " + path, durLength, durLength.Length, 0); APIClass.mciSendString("close My", durLength, durLength.Length, 0); } catch (Exception ex) { ex.Message.ToString(); } } /// <summary> /// 停止录音 /// </summary> public void StopRecord() { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); try { APIClass.mciSendString("stop My", durLength, durLength.Length, 0); } catch (Exception ex) { ex.Message.ToString(); } } /// <summary> /// 暂停录音 /// </summary> public void PauseRecord() { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); try { APIClass.mciSendString("pause My", durLength, durLength.Length, 0); } catch (Exception ex) { ex.Message.ToString(); } } /// <summary> /// 继续录音 /// </summary> public void ResumeRecord() { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); try { APIClass.mciSendString("resume My", durLength, durLength.Length, 0); } catch (Exception ex) { ex.Message.ToString(); } } public class APIClass { [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName(string lpszLongPath, string shortFile, int cchBuffer); [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)] public static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback); }
作 者:大師兄丶
出 处:http://www.cnblogs.com/zhao-yi
Git 地 址:https://github.com/ZhaoYis
个人博客:http://www.zhaoyis.com.cn
关于作者:主要从事基于.Net Framework平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!
出 处:http://www.cnblogs.com/zhao-yi
Git 地 址:https://github.com/ZhaoYis
个人博客:http://www.zhaoyis.com.cn
关于作者:主要从事基于.Net Framework平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!