Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NAudio Waveilewriter不';t将文件大小写入wave文件_C#_Wav - Fatal编程技术网

C# NAudio Waveilewriter不';t将文件大小写入wave文件

C# NAudio Waveilewriter不';t将文件大小写入wave文件,c#,wav,C#,Wav,我试图用NAudio用WasapiLoopbackCapture和WaveileWriter用C#录制一些声音 问题是录制完成后,WAV/RIFF头中的“大小”字段设置为0,导致文件无法播放 我正在使用以下代码: WasapiLoopbackCapture CaptureInstance = null; WaveFileWriter RecordedAudioWriter = null; void StartSoundRecord() { str

我试图用NAudio用WasapiLoopbackCapture和WaveileWriter用C#录制一些声音

问题是录制完成后,WAV/RIFF头中的“大小”字段设置为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();
        }
    }
(借用自:)

我正在测试的只是:

    StartSoundRecord();
    Thread.Sleep(10000);
    StopSoundRecord();
我遗漏了什么,为什么WaveFileWriter不写大小字段?在处理之前,我还尝试调用Flush()和Close()方法。但这没什么区别

当然,我可以编写一个方法来找出文件的大小并手动将其写入最终文件,但这似乎是不必要的。

找到了解决方案

每次写入后调用RecordedAudioWriter.Flush()使其工作正常

不知道这样做是否效率低下(因为我假设在将数据写入磁盘之前flush是阻塞的),但对于我的应用程序来说,这不是问题