Recording I generally prefer the MCI approach and of course there is another way and DirectSound but it's going to be a lot of trouble.
But it has a feature that makes me feel better, first you can choose the source of recording equipment, second it is not like MCI
Must write to the local disk, it is to put the recorded audio stream in memory, at the speed I think it will quickly
Much, but it also has a drawback that makes me uncomfortable, it's not as easy as MCI which is integrated in the system by default, I this
Personal preference to use the system comes with functions, mainly you do not need to copy the DLL because the system is the default,
Just need to go to call on the line and of course the MCI provides operational commands I hate mcisendcommand.
The reason is that I have to write a lot of structure is very annoying to me, mcisendstring is through
String, which belongs to the light-weight operation function I prefer the tight.
Private Soundrecord M_record = new Soundrecord (); private void Mainform_load (object sender, EventArgs e) { this.m_Record.OpenRecord ();///Open recording this.m_ Record.startrecord (); Start recording } private void Btnstopandsave_click (object sender, EventArgs e) { This.m_Record.StopRecord () ; Stop recording This.m_Record.SaveRecord (@ "C:\Users\windo\Desktop\1.wav");//Save Recording This.m_Record.CloseRecord (); /Close Recording }
The above code is just a simple recording and saving process, but the class itself is very simple and I also attach
There are comments, presumably you should not feel the learning difficulties, the recording property configuration is initialized in the constructor by default.
Public partial class Soundrecord {[DllImport ("WinMm.dll", CharSet = CharSet.Auto, callingconvention = Calli NGCONVENTION.CDECL)] private static extern int mcisendstring (string lpstrcommand, string lpstrreturnstring, int uRe turnlength, 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)! = E rror_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; }//number of sample bits public virtual int BitsPerSample { set {if (mciSendString ("Set wave bitpersample" + value)) this.m_bits_per_s ample = value; } get {return this.m_bits_per_sample; }}//sampling frequency public virtual int SamplesPerSec {get {Retu RN This.m_sample_spersec; } set {if (mciSendString ("Set wave samplespersec" + value) this . m_sample_spersec = value; }}//channel public virtual int Channels {get {return m_ch Annels; } set {if (mciSendString ("Set wave channels" + value)) This.m_ch Annels = value; }}//Format label public virtual string Formattag {get {retur n This.m_format_tag; } set {if (mciSendString ("Set wave format tag" + value) this.m_fo Rmat_tag = value; }}//Open recording public virtual bool Openrecord () {return mcisendstring ("open new type Waveaudio alias movie "); }//Start recording public virtual bool Startrecord () {return mcisendstring ("record movie"); }//Stop recording public virtual bool Stoprecord () {return mcisendstring ("Stop movie"); }//Save recording public virtual bool Saverecord (string savefilename) {return mcisendstring ("Sav E movie "+ Savefilename); }//Turn off recording public virtual bool Closerecord () {return mcisendstring ("close movie"); }//Pause recording public virtual bool Pauserecord () {return mcisendstring ("Pause movie"); }//Recover recording public virtual bool ResumerecOrd () {return mcisendstring ("Resume movie"); }//Execution state public virtual string Status {get {string strbuffer = new String (' + ', 12); if (mcisendstring ("Status movie Mode", Strbuffer, 0)! = ERROR_SUCCESS) return mode_unknown; if (Strbuffer = Strbuffer.remove ("Strbuffer.indexof (')"). Length <= 0) return mode_unknown; return strbuffer; } } }
C # MCI Soundrecord/Recording