使用winmm.dll 获取麦克风声音数据

//录音
        /// 
        /// 初始化录音环境
        /// 
        /// 
        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;
        }

        /// 
        /// 开始录音
        /// 
        /// 
        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;
        }

        /// 
        /// 保存录音
        /// 
        /// 保存路径
        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();
            }
        }

        /// 
        /// 停止录音
        /// 
        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();
            }
        }

        /// 
        /// 暂停录音
        /// 
        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();
            }
        }

        /// 
        /// 继续录音
        /// 
        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);
    }

  

转载于:https://www.cnblogs.com/zhao-yi/p/6225940.html

你可能感兴趣的:(使用winmm.dll 获取麦克风声音数据)