C# NAudio:录制的文件无法播放

C# NAudio: Recorded file wont play

我正在尝试录制我的声音,并在停止录制时弹出一个保存文件对话框。我可以这样做并将其保存到我的桌面以供播放。不幸的是,Groove 无法识别该文件(声明它无法播放),我很肯定这不是他们的错。

请原谅评论和代码转储。

public partial class RecordAndEditVoice : Window
{
 WaveIn sourceStream = null;
    WaveFileWriter waveWriter = null;
      SaveFileDialog save = new SaveFileDialog();

     private void sourceStream_DataAvailable(object sender, WaveInEventArgs e)
    {
        if (waveWriter == null) return;
        //Adds bytes to the wave file, storing them in a buffer? 
        waveWriter.Write(e.Buffer, 0, e.BytesRecorded);
        waveWriter.Flush();
    }

    private void recordButton_Click(object sender, RoutedEventArgs e)
    {


        MainWindow mw = new MainWindow();
        int devNum = mw.DeviceButton();

        if (recordButton.Content.ToString() == "RECORD")
        {
            recordButton.Content = "STOP";
            //Start recording audio. 
            sourceStream = new WaveIn();

            sourceStream.DeviceNumber = devNum;

            //1. Set the sample rate. 2. Get number of channels supported on the device.
            sourceStream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(devNum).Channels);



            sourceStream.StartRecording();

            sourceStream.DataAvailable += new EventHandler<WaveInEventArgs>(sourceStream_DataAvailable);


        }
        else if(recordButton.Content.ToString() == "STOP")
        {

            sourceStream.StopRecording();
            save.Filter = "Wave Files (*.wav)|*.wav;";
            if (save.ShowDialog() != true) return;

            waveWriter = new WaveFileWriter(save.FileName, sourceStream.WaveFormat);


            recordButton.Content = "RECORD";



        }
    }
}

我尝试将 waveWriter 移动到 if 语句中,但它抛出了 'System.ArgumentException' 类型的异常。当用谷歌搜索时,引用范围太广,没有多大帮助。

您必须在 WaveFileWriter 上调用 Dispose 才能获得可播放的文件。这是因为 WAV 文件格式在文件开头包含一些长度信息,这些信息仅在您调用 Dispose

时填写