• C# recording audio based on audio in Console


    1.

    Install-package naudio -v 1.9.0

    2.

    using NAudio.Wave;

    3.

     public class NAudioHelper
        {        
            public WaveInEvent mWavIn;
            public WaveFileWriter mWavWriter;
    
            public bool IsStoppedRecording { get; set; } = false;
            /// <summary>
            /// starting  recording
            /// </summary>
            /// <param name="filePath"></param>
            public void StartRecord(string filePath)
            {
                mWavIn = new WaveInEvent();
                mWavIn.DataAvailable += MWavIn_DataAvailable;           
                mWavWriter = new WaveFileWriter(filePath, mWavIn.WaveFormat);
                mWavIn.StartRecording();
    
                ConsoleKeyInfo cki = Console.ReadKey(true);            
                while (cki.Key == ConsoleKey.Escape)
                {
                    //Environment.Exit(0);
                    StopRecord();
                    IsStoppedRecording = true;
                    if(IsStoppedRecording)
                    {
                        Console.ForegroundColor = ConsoleColor.Red; 
                        Console.WriteLine("Stopped recording!");
                        break;
                    }
                }            
            }
    
            /// <summary>
            /// stop recording
            /// </summary>
            public void StopRecord()
            {
                mWavIn?.StopRecording();
                mWavIn?.Dispose();
                mWavIn = null;
                mWavWriter?.Close();
                mWavWriter = null;
            }      
    
            private void MWavIn_DataAvailable(object sender, WaveInEventArgs e)
            {
                mWavWriter.Write(e.Buffer, 0, e.BytesRecorded);
                int secondsRecorded = (int)mWavWriter.Length / mWavWriter.WaveFormat.AverageBytesPerSecond;
            }
        }
    
    
    static void Main(string[] args)
            {
                NAudioDemo();      
            }
             
            static void NAudioDemo()
            {
                NAudioHelper recordHelper = new NAudioHelper();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Ready!");
                Console.WriteLine("Enter Enter Key to start recording!");
                ConsoleKeyInfo info = Console.ReadKey(true);  
    
                bool isStarted = false;
                while (info.Key==ConsoleKey.Enter)
                {
                    if (!isStarted)
                    {
                        string recordedFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".mp3";
                        Console.WriteLine("Start recording...");
                        Console.WriteLine("Press the Escape(Esc) key to quit: 
    ");
                        recordHelper.StartRecord(recordedFileName);                    
                        isStarted = true;
                    }                
                }           
            }     
  • 相关阅读:
    python程序设计练习题:电子银行购物商城
    python -m参数
    Python 中的 if __name__ == 'main' 的作用和原理
    Modbus协议
    Python configparser模块
    Python logging 模块:日志处理
    python hashlib模块
    Python time模块:Python 日期和时间
    mac 使用系统的自带的quickTime录屏
    Linphone android3.2.4 采用率设置(模拟电话大网信道)
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11891464.html
Copyright © 2020-2023  润新知