Recording with NAudio using C#

前端 未结 2 1542
刺人心
刺人心 2021-02-06 13:42

I am trying to record audio in C# using NAudio. After looking at the NAudio Chat Demo, I used some code from there to record.

Here is the code:

using Sys         


        
2条回答
  •  情话喂你
    2021-02-06 14:02

    If this is your whole code, then you are missing a message loop. All the eventHandler specific events requires a message loop. You can add a reference to Application or Form as per your need.

    Here is an example by using Form:

    using System;
    using System.Windows.Forms;
    using System.Threading;
    using NAudio.Wave;
    
    public class FOO
    {
        static WaveIn s_WaveIn;
    
        [STAThread]
        static void Main(string[] args)
        {
            Thread thread = new Thread(delegate() {
                init();
                Application.Run();
            });
    
            thread.Start();
    
            Application.Run();
        }
    
        public static void init()
        {
            s_WaveIn = new WaveIn();
            s_WaveIn.WaveFormat = new WaveFormat(44100, 2);
    
            s_WaveIn.BufferMilliseconds = 1000;
            s_WaveIn.DataAvailable += new EventHandler(SendCaptureSamples);
            s_WaveIn.StartRecording();
        }
    
        static void SendCaptureSamples(object sender, WaveInEventArgs e)
        {
            Console.WriteLine("Bytes recorded: {0}", e.BytesRecorded);
        }
    }
    

提交回复
热议问题