C#中NAudio音頻庫的安裝與使用教程詳解
NAudio安裝
項目=>NuGet包管理器 搜索NAudio點擊安裝,自動安裝依賴庫。
安裝成功后工具箱會新增NAudio.WinForms控件
NAudio簡述
NAudio為.NET平臺下的開源庫,采用ML-PL協(xié)議,開源地址:https://github.com/naudio/NAudio支持多種音頻操作,可實現(xiàn)多種API播放與錄制、多種不同音頻格式、音頻格式轉換(重采樣、位深、聲道等)、音頻編碼、多通道播放、音頻效果處理等等。
常用類:
- WaveIn 表示波形輸入, 繼承了 IWaveIn, 例如麥克風輸入, 或者計算機正在播放的音頻流。
- WaveOut 表示波形輸出, 繼承了 IWavePlayer, 用來播放音頻, 以 IWaveProvider 作為播放源播放音頻, 通過拓展方法也支持以 ISampleProvider 作為播放源播放音頻。
- WaveStream 表示波形流, 它繼承了 IWaveProvider, 可以用來作為播放源。
- WaveFileReader 繼承了 WaveStream, 用來讀取波形文件。
- WaveFileWriter 繼承了Stream, 用來寫入文件, 常用于保存音頻錄制的數據。
- AudioFileReader 通用的音頻文件讀取器, 可以讀取波形文件, 也可以讀取其他類型的音頻文件例如 Aiff, MP3
常用接口:
- IWaveProvider 波形提供者, 上面已經提到, 是音頻播放的提供者, 通過拓展方法可以轉換為 ISampleProvider。
- ISampleProvider 采樣提供者, 上面已經提到, 通過拓展方法可以作為 WaveOut 的播放源。
簡單示例1
自定義錄音機類:Recorder.cs
using NAudio.Wave; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NAudioDemo { internal class Recorder { public WaveIn mWaveIn; public WaveFileWriter mWaveFileWriter; public int secondsRecorded; /// <summary> /// 開始錄音 /// </summary> /// <param name="filePath"></param> public void RecorderStart(string filePath) { // 創(chuàng)建WaveIn對象 mWaveIn = new WaveIn(); // 添加DataAvailable事件處理回調 mWaveIn.DataAvailable += OnDataAvailable; // 創(chuàng)建WaveFileWriter對象 mWaveFileWriter = new WaveFileWriter(filePath, mWaveIn.WaveFormat); // 開始錄音 mWaveIn.StartRecording(); } /// <summary> /// 停止錄音 /// </summary> public void RecorderStop() { mWaveIn?.StopRecording(); mWaveIn?.Dispose(); mWaveFileWriter?.Close(); mWaveFileWriter = null; } /// <summary> /// 錄音數據回調 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnDataAvailable(object sender, WaveInEventArgs e) { // 寫入錄音數據 mWaveFileWriter.Write(e.Buffer, 0, e.BytesRecorded); // 計算已錄制的秒數 secondsRecorded = (int)mWaveFileWriter.Length / mWaveFileWriter.WaveFormat.AverageBytesPerSecond; } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using NAudio.Wave; using System.Drawing.Text; using System.Media; using NAudio.Dsp; namespace NAudioDemo { public partial class Form1 : Form { // 創(chuàng)建錄音機類實例 Recorder recorder = new Recorder(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; button2.Enabled = true; recorder.RecorderStart(@"D:\1.wav"); } private void button2_Click(object sender, EventArgs e) { button1.Enabled = true; button2.Enabled = false; recorder.RecorderStop(); } private void button3_Click(object sender, EventArgs e) { SoundPlayer player = new SoundPlayer(@"D:\1.wav"); player.Play(); } } }
錄制麥克風
借助 WaveIn 類, 我們可以輕易的捕獲麥克風輸入, 在每一次錄制到數據時, 將數據寫入到文件或其他流, 這就實現(xiàn)了保存錄音
在保存波形文件時需要借助 WaveFileWriter, 當然, 如果你想保存為其他格式, 也可以使用其它的 Writer, 例如 CurWaveFileWriter 以及AiffFileWriter, 美中不足的是沒有直接寫入到 MP3 的 FileWriter
需要注意的是, 桌面程序可以直接使用 WaveIn, 其回調基于 Windows 消息, 所以無法在控制臺應用中使用 WaveIn
如果要在控制臺應用中實現(xiàn)錄音, 只需要使用 WaveInEvent, 它的回調基于事件而不是 Windows 消息, 所以可以通用
示例代碼:
WaveIn cap = new WaveIn(); // cap, capture WaveFileWriter writer = new WaveFileWriter(); cap.DataAvailable += (s, args) => writer.Write(args.Buffer, 0, args.BytesRecorded); // 訂閱事件 cap.StartRecording(); // 開始錄制 cap.StopRecording(); // 停止錄制 writer.Close(); // 關閉 FileWriter, 保存數據
另外, 除了使用 WaveIn, 你還可以使用 WasapiCapture, 它與 WaveIn 的使用方式是一致的, 可以用來錄制麥克風
Wasapi 全稱 Windows Audio Session Application Programming Interface (Windows音頻會話應用編程接口)
具體 WaveIn, WaveInEvent, WasapiCapture 的性能, 筆者還沒有測試過, 但估計不會有太大差異.
提示: WasapiCapture 和 WasapiLoopbackCapture 位于 NAudio.Wave 命名空間下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using NAudio; using NAudio.Wave; namespace NAudioDemo2 { public partial class Form2 : Form { private WaveIn waveIn = null; private WaveFileWriter writer = null; public Form2() { InitializeComponent(); button2.Enabled = false; button3.Enabled = false; } /// <summary> /// 設置保存文件名稱 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); saveFileDialog1.Filter = "audio files (*.wav)|*.wav| all files (*.*)|*.*"; // 文件類型過濾 saveFileDialog1.DefaultExt = "*.wav"; // 默認文件擴展名 //saveFileDialog1.FileName = "1.wav"; // 默認文件名 if (saveFileDialog1.ShowDialog() == DialogResult.OK) { string fName = saveFileDialog1.FileName; // 獲取文件名 textBox1.Text = fName; button2.Enabled = true; } } /// <summary> /// 開始錄音 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { //waveIn = new WaveIn { WaveFormat = new WaveFormat(44100, 1) }; waveIn = new WaveIn(); waveIn.WaveFormat = new WaveFormat(44100, 1); writer = new WaveFileWriter(textBox1.Text.Trim(), waveIn.WaveFormat); waveIn.DataAvailable += (s, args) => writer.Write(args.Buffer, 0, args.BytesRecorded); waveIn.StartRecording(); button2.Enabled = false; button3.Enabled = true; } /// <summary> /// 停止錄音 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { waveIn.StopRecording(); waveIn.Dispose(); waveIn = null; writer.Flush(); writer.Close(); writer.Dispose(); button2.Enabled = true; button3.Enabled = false; } } }
錄制系統(tǒng)聲卡
錄制聲卡輸出, 也就是錄制計算機正在播放的聲音, 借助 WasapiLoopbackCapture 即可簡單實現(xiàn), 使用方式與 WasapiCapture 無異
WasapiLoopbackCapture cap = new WasapiLoopbackCapture(); WaveFileWriter writer = new WaveFileWriter(); cap.DataAvailable += (s, args) => writer.Write(args.Buffer, 0, args.BytesRecorded); cap.StartRecording();
using NAudio.Wave; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace NAudioDemo2 { public partial class Form3 : Form { private WasapiLoopbackCapture loopCap = new WasapiLoopbackCapture(); private WaveFileWriter fileWriter; public Form3() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "wave file *.wav|*.wav|all filse *.*|*.*"; saveFileDialog.DefaultExt = ".wav"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { string fName = saveFileDialog.FileName; textBox1.Text = fName; } } /// <summary> /// 錄制聲卡開始 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { fileWriter = new WaveFileWriter(textBox1.Text.Trim(), loopCap.WaveFormat) ; loopCap.DataAvailable += (s, args) => fileWriter.Write(args.Buffer, 0, args.BytesRecorded); loopCap.StartRecording(); } /// <summary> /// 錄制聲卡停止 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { loopCap.StopRecording(); fileWriter.Flush(); fileWriter.Close(); fileWriter.Dispose(); } } }
WAV格式播放
NAudio 中, 通過 WaveFileReader 來讀取波形數據, 在實例化時, 你可以指定文件名或者是輸入流, 這意味著你可以讀取內存流中的音頻數據.
WaveFileReader reader = new WaveFileReader(filepath); WaveOut wout = new WaveOut(); wout.Init(reader); // 通過 IWaveProvider 為音頻輸出初始化 wout.Play(); // 至此, wout 將從指定的 reader 中提供的數據進行播放
using NAudio.Wave; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace NAudioDemo2 { public partial class Form4 : Form { private WaveOut wout_wav; WaveFileReader reader; public Form4() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "audio file *.wav|*.wav"; ofd.DefaultExt = "*.wav"; if (ofd.ShowDialog() == DialogResult.OK) { string fName = ofd.FileName; textBox1.Text = fName; } } private void button2_Click(object sender, EventArgs e) { reader = new WaveFileReader(textBox1.Text.Trim()); wout_wav = new WaveOut(); wout_wav.Init(reader); wout_wav.Play(); } private void button3_Click(object sender, EventArgs e) { wout_wav.Pause(); } private void button4_Click(object sender, EventArgs e) { wout_wav.Resume(); } private void button5_Click(object sender, EventArgs e) { wout_wav.Stop(); wout_wav.Dispose(); reader.Close(); } } }
MP3格式播放
播放 MP3 音樂其實與播放波形音樂沒有太大區(qū)別, 只不過將 WaveFileReader 換成Mp3FileReader 罷了
Mp3FileReader reader = new Mp3FileReader(filepath); WaveOut wout = new WaveOut(); wout.Init(reader); wout.Play();
using NAudio.Wave; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace NAudioDemo2 { public partial class Form5 : Form { Mp3FileReader reader; WaveOut wout_mp3; public Form5() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "audio file *.mp3|*.mp3"; ofd.DefaultExt = "*.mp3"; if (ofd.ShowDialog() == DialogResult.OK) { string fName = ofd.FileName; textBox1.Text = fName; } } private void button2_Click(object sender, EventArgs e) { reader = new Mp3FileReader(textBox1.Text); wout_mp3 = new WaveOut(); wout_mp3.Init(reader); wout_mp3.Play(); } private void button3_Click(object sender, EventArgs e) { wout_mp3.Pause(); } private void button4_Click(object sender, EventArgs e) { wout_mp3.Resume(); } private void button5_Click(object sender, EventArgs e) { wout_mp3.Stop(); wout_mp3.Dispose(); reader.Close(); reader.Dispose(); } } }
AudioFileReader讀取播放音頻
通過AudioFileReader讀取音頻文件可以播放.mp3, .wav, .flac等多種格式
AudioFileReader reader = new AudioFileReader (@“d:\1.mp3”); WaveOut wout = new WaveOut (); wout.Init(reader ); wout.Play(); wout.Pause(); wout.Resume(); wout.Stop();
using NAudio.Wave; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace NAudioDemo2 { public partial class Form6 : Form { AudioFileReader reader; WaveOut wout; public Form6() { InitializeComponent(); } private void Form6_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "audio file *.mp3|*.mp3|all files *.*|*.*"; if (ofd.ShowDialog() == DialogResult.OK) { string fName = ofd.FileName; textBox1.Text = fName; } } private void button2_Click(object sender, EventArgs e) { reader = new AudioFileReader(textBox1.Text.Trim()); wout = new WaveOut(); wout.Init(reader); wout.Play(); } private void button3_Click(object sender, EventArgs e) { wout.Pause(); } private void button4_Click(object sender, EventArgs e) { wout.Resume(); } private void button5_Click(object sender, EventArgs e) { wout.Stop(); wout.Dispose(); reader.Close(); reader.Dispose(); } } }
MediaFoundationReader 讀取播放音頻
通過MediaFoundationReader 讀取音頻文件可以播放.mp3, .wav, .flac等多種格式
MediaFoundationReader reader = new MediaFoundationReader (@“d:\1.mp3”); WaveOut wout = new WaveOut (); wout.Init(reader ); wout.Play(); wout.Pause(); wout.Resume(); wout.Stop();
using NAudio.Wave; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace NAudioDemo2 { public partial class Form7 : Form { MediaFoundationReader reader; WaveOut wout; public Form7() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "audio file *.mp3|*.mp3|all files *.*|*.*"; ofd.DefaultExt = "*.mp3"; if (ofd.ShowDialog() == DialogResult.OK) { string fName = ofd.FileName; textBox1.Text = fName; } } private void button2_Click(object sender, EventArgs e) { reader = new MediaFoundationReader(textBox1.Text.Trim()); wout = new WaveOut(); wout.Init(reader); wout.Play(); } private void button3_Click(object sender, EventArgs e) { wout.Pause(); } private void button4_Click(object sender, EventArgs e) { wout.Resume(); } private void button5_Click(object sender, EventArgs e) { wout.Stop(); wout.Dispose(); reader.Close(); reader.Dispose(); } } }
以上就是C#中NAudio音頻庫的安裝與使用教程詳解的詳細內容,更多關于C# NAudio的資料請關注腳本之家其它相關文章!
相關文章
使用PageHelper插件實現(xiàn)Service層分頁
這篇文章主要為大家詳細介紹了使用PageHelper插件實現(xiàn)Service層分頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04java中的Consumer、Supply如何實現(xiàn)多參數?
Java的Consumer接口只能接受一個參數,但可以通過自定義接口、使用Tuple或嵌套結構來實現(xiàn)對多個參數的處理,對于Supplier接口,它不能接受參數,但可以通過自定義BiSupplier、結合Function或封裝參數為對象來實現(xiàn)對兩個參數并返回一個值的功能2024-11-11MybatisPlus字段自動填充失效,填充值為null的解決方案
這篇文章主要介紹了MybatisPlus字段自動填充失效,填充值為null的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01