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: \n"); recordHelper.StartRecord(recordedFileName); isStarted = true; } } }
原文链接: https://www.cnblogs.com/Fred1987/p/11891464.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/402121
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!