How to start and stop audio recorder on its own in c# ( using NAudio) RRS feed

  • Question

  • Hey all 

    I am working on record audio using NAudio in C# and I need to start recording automatically and stop it automatically also after 6 <g class="gr_ gr_18 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="18" id="18">sec  .</g> 
     It works as for starting but not stop 

    This the code 

    public partial class Form1 :


    System.Windows.Forms.Timer mytimer = new System.Windows.Forms.Timer(); //create a new Timer    

    private WaveFileWriter RecordedAudioWriter = null; 

      private WasapiLoopbackCapture CaptureInstance = null; 

      public Form1()    {     


        mytimer.Interval = 6000; //set the interval to 1 second.   

        mytimer.Tick += new EventHandler(mytimer_Tick);     

       mytimer.Enabled = true; 


    private void mytimer_Tick(object sender, EventArgs e)    {     


      string outputFilePath = @"D:\deep\New\Transfare\tf-File\output\out.wav";   

        // Redefine the capturer instance with a new instance of the LoopbackCapture class   

        this.CaptureInstance = new WasapiLoopbackCapture();     

      // Redefine the audio writer instance with the given configuration     

      this.RecordedAudioWriter = new WaveFileWriter(outputFilePath, CaptureInstance.WaveFormat);   

        // When the capturer receives audio, start writing the buffer into the mentioned file     

      this.CaptureInstance.DataAvailable += (s, a) =>       


            this.RecordedAudioWriter.Write(a.Buffer, 0, a.BytesRecorded);   


    // When the Capturer Stops     

      this.CaptureInstance.RecordingStopped += (s, a) =>     



              this.RecordedAudioWriter = null;   

            CaptureInstance.Dispose();        }; 

          // Start recording !   



    private void stoprec(object sender, EventArgs e)   



        this.mytimer.Enabled = false;     



    Could you please help me 


    Wednesday, November 21, 2018 6:14 PM

All replies

  • Hi safaa,

    According to your description and related code, it seems that you issue is more related to NAudio, I would suggest that you could post your issue on github of NAudio for suitable support.


    Thanks for your understanding.

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 22, 2018 2:46 AM