简体   繁体   English

NAudio WaveFileWriter 不会将文件大小写入波形文件

[英]NAudio WaveFileWriter doesn't write file size to wave file

I'm trying to use NAudio to record some sound in C# using WasapiLoopbackCapture and WaveFileWriter.我正在尝试使用 NAudio 使用 WasapiLoopbackCapture 和 WaveFileWriter 在 C# 中录制一些声音。

The problem is that after the recording is finished, the "size" field in the WAV/RIFF header is set to 0, rendering the file unplayable.问题是录制完成后,WAV/RIFF 头中的“size”字段设置为0,导致文件无法播放。

I'm using the following code:我正在使用以下代码:

    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();
        }
    }

(Borrowed from: https://ourcodeworld.com/articles/read/702/how-to-record-the-audio-from-the-sound-card-system-audio-with-c-using-naudio-in-winforms ) (借用: https : //ourcodeworld.com/articles/read/702/how-to-record-the-audio-from-the-sound-card-system-audio-with-c-using-naudio-in- winforms )

Which I'm testing with simply:我正在简单地测试:

    StartSoundRecord();
    Thread.Sleep(10000);
    StopSoundRecord();

What am I missing, why isn't WaveFileWriter writing the size field?我错过了什么,为什么 WaveFileWriter 不写大小字段? I have tried also calling the Flush() and Close() methods before disposing.在处理之前,我也尝试过调用 Flush() 和 Close() 方法。 But it makes no difference.但这没什么区别。

Sure, I could write a method to find out the size of the file and manually writing it to the final file, but that seems unnecessary.当然,我可以编写一个方法来找出文件的大小并手动将其写入最终文件,但这似乎没有必要。

Found the solution.找到了解决办法。

Calling RecordedAudioWriter.Flush() after every Write made it work just fine.每次写入后调用 RecordedAudioWriter.Flush() 使其工作正常。

Don't know if doing so might be inefficient (as I assume flush is blocking until data is written to disk), but for my application that is not an issue.不知道这样做是否可能效率低下(因为我假设在数据写入磁盘之前刷新是阻塞的),但对于我的应用程序来说这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM