C# mci SoundRecord / 錄音

jopen 9年前發布 | 2K 次閱讀 C#

錄音我一般更喜歡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;
            }
        }
    }


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!