[英]Naudio generated audio wave form doesn't match the sound for some .wav file and .m4a file
[英]NAudio WaveFileWriter doesn't write file size to wave file
我正在嘗試使用 NAudio 使用 WasapiLoopbackCapture 和 WaveFileWriter 在 C# 中錄制一些聲音。
問題是錄制完成后,WAV/RIFF 頭中的“size”字段設置為0,導致文件無法播放。
我正在使用以下代碼:
WasapiLoopbackCapture CaptureInstance = null;
WaveFileWriter RecordedAudioWriter = null;
void StartSoundRecord()
{
string outputFilePath = @"C:\RecordedSound.wav";
// Redefine the capturer instance with a new instance of the LoopbackCapture class
CaptureInstance = new WasapiLoopbackCapture();
// Redefine the audio writer instance with the given configuration
RecordedAudioWriter = new WaveFileWriter(outputFilePath, CaptureInstance.WaveFormat);
// When the capturer receives audio, start writing the buffer into the mentioned file
CaptureInstance.DataAvailable += (s, a) =>
{
// Write buffer into the file of the writer instance
RecordedAudioWriter.Write(a.Buffer, 0, a.BytesRecorded);
};
// When the Capturer Stops, dispose instances of the capturer and writer
CaptureInstance.RecordingStopped += (s, a) =>
{
RecordedAudioWriter.Dispose();
RecordedAudioWriter = null;
CaptureInstance.Dispose();
};
// Start audio recording !
CaptureInstance.StartRecording();
}
void StopSoundRecord()
{
if(CaptureInstance != null)
{
CaptureInstance.StopRecording();
}
}
(借用: https : //ourcodeworld.com/articles/read/702/how-to-record-the-audio-from-the-sound-card-system-audio-with-c-using-naudio-in- winforms )
我正在簡單地測試:
StartSoundRecord();
Thread.Sleep(10000);
StopSoundRecord();
我錯過了什么,為什么 WaveFileWriter 不寫大小字段? 在處理之前,我也嘗試過調用 Flush() 和 Close() 方法。 但這沒什么區別。
當然,我可以編寫一個方法來找出文件的大小並手動將其寫入最終文件,但這似乎沒有必要。
找到了解決辦法。
每次寫入后調用 RecordedAudioWriter.Flush() 使其工作正常。
不知道這樣做是否可能效率低下(因為我假設在數據寫入磁盤之前刷新是阻塞的),但對於我的應用程序來說這不是問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.