使用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);
    }

  

posted @ 2016-12-27 15:01  大师兄丶  阅读(894)  评论(0编辑  收藏  举报