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

    Pertanyaan

  • 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 :

    Form{   

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

    private WaveFileWriter RecordedAudioWriter = null; 

      private WasapiLoopbackCapture CaptureInstance = null; 

      public Form1()    {     

      InitializeComponent();   

        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)    {     

      mytimer.Start();     

      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.Dispose(); 

              this.RecordedAudioWriter = null;   

            CaptureInstance.Dispose();        }; 

          // Start recording !   

        this.CaptureInstance.StartRecording(); 

          stoprec(sender,e);   

    private void stoprec(object sender, EventArgs e)   

      {     

      this.CaptureInstance.StopRecording();   

        this.mytimer.Enabled = false;     

        this.Close();     

    }

    Could you please help me 

    Thanks 


    Rabu, 21 November 2018 18.14

Semua Balasan

  • 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.

    https://github.com/naudio/NAudio/pulls

    Thanks for your understanding.

    Best regards,

    Zhanglong


    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.

    Kamis, 22 November 2018 02.46