NAudio录音..以下代码没有正确写入wav文件

时间:2013-04-29 20:32:30

标签: c# naudio

以下是我使用naudio的录音方法。它会创建wav文件,但不会将声音数据写入其中。我无法弄清楚我们出了什么问题。记录后总是wav文件大小小于1kb。这是我的代码

    public void recordMe(String selectedDevice)
    {

        int waveInDevices = WaveIn.DeviceCount;
        int waveInDevice;
        for (waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
        {
            WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);

            if (deviceInfo.ProductName == selectedDevice)
            {

                break;
            }
        }

        int sampleRate = 8000;
        int channels = 1;

        wi.WaveFormat = new WaveFormat(sampleRate, channels);
        wi.DeviceNumber = waveInDevice;

        writer = new WaveFileWriter(outputFilename, wi.WaveFormat);
        wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);
        //add to tell record after the beep
        (new SoundPlayer(@"For Beep\please pronounce the text after the beep.wav")).Play();
        Thread.Sleep(3000);
        (new SoundPlayer(@"For Beep\beep.wav")).Play(); 
        wi.StartRecording();
        Thread.Sleep(10000);
        wi.StopRecording();
        wi.Dispose();
        writer.Dispose();
        writer.Close();
    }
    void wi_DataAvailable(object sender, WaveInEventArgs e)
    {
        writer.WriteData(e.Buffer, 0, e.BytesRecorded);
    }

0 个答案:

没有答案