SoFunction
Updated on 2025-04-04

C# realizes computer microphone recording

This example completes microphone recording through the Naudio library and saves the PCM pulse signal into a wav audio file. For audio sampling rate, bit rate, channel and other issues, please refer to the relevant information, and this example will not be explained. Naudio library Please search and download from NuGet.

Recording interface class:

public interface ISpeechRecorder
    {
        void SetFileName(string fileName);
        void StartRec();
        void StopRec();
    }

Recording implementation class:

using System;
using ;

namespace Test
{
    class NAudioRecorder : ISpeechRecorder
    {

        public WaveIn waveSource = null;
        public WaveFileWriter waveFile = null;
        private string fileName = ;

        /// <summary>
        /// Step 2: Start recording        /// </summary>
        public void StartRec()
        {
            try
            {
                waveSource = new WaveIn();// Make sure that the computer has microphone access, otherwise an error will be reported.                 = new WaveFormat(16000, 16, 1); // 16KHz, 16bit, mono recording format
                 += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
                 += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);

                waveFile = new WaveFileWriter(fileName, );

                ();
            }
            catch(Exception e)
            {
                throw new Exception();
            }
            
        }

        /// <summary>
        /// Step 3: Stop recording        /// </summary>
        public void StopRec()
        {
            ();

            // Close Wave(Not needed under synchronous situation)
            if (waveSource != null)
            {
                ();
                waveSource = null;
            }

            if (waveFile != null)
            {
                ();
                waveFile = null;
            }
        }

        /// <summary>
        /// Step 1: Set the file path to save after recording is finished        /// </summary>
        /// <param name="fileName">Save the path name of the wav file</param>        public void SetFileName(string fileName)
        {
             = fileName;
        }

        /// &lt;summary&gt;
        /// Start recording callback function        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;&lt;/param&gt;
        /// &lt;param name="e"&gt;&lt;/param&gt;
        private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (waveFile != null)
            {
                (, 0, );
                ();
            }
        }

        /// &lt;summary&gt;
        /// The recording end callback function        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;&lt;/param&gt;
        /// &lt;param name="e"&gt;&lt;/param&gt;
        private void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
        {
            if (waveSource != null)
            {
                ();
                waveSource = null;
            }

            if (waveFile != null)
            {
                ();
                waveFile = null;
            }
        }
    }
}

Calling method:

NAudioRecorder nar=new NAudioRecorder();
(@"d:\");
();

();

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.