0
votes

im having problems when trying to record sound from microphone in my wpf application. here's my code:

    public NAudio.Wave.WaveFileWriter wav_writer = null;  // snemanje
    public WaveIn strim;

    private void snemanje_Click(object sender, RoutedEventArgs e)
    {
        strim = new WaveIn();
        strim.WaveFormat = new WaveFormat(44100, 0);

        SaveFileDialog svg = new SaveFileDialog();
        svg.Filter = "*WAV File (*.wav)|*.wav;*";
        svg.ShowDialog();

        strim.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable);
        wav_writer = new WaveFileWriter(svg.FileName, strim.WaveFormat);
        strim.StartRecording();
    }


    public void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
    {
        wav_writer.WriteData(e.Buffer, 0, e.BytesRecorded);
        wav_writer.Flush();
    }

everything works perfectly and the file is saved in the projects location, but i can not oppen it. it says that it is a wave file but everytime i want to play it, it says that i must find codecs for playing in that format (but there arent any).

1
it doesnt work.. dont know why.. if I record for example for 4s the file size iz 487kb, that's ok i guess.. - TreeZ

1 Answers

0
votes

Don't create a WaveFormat with 0 channels. You should use 1 for mono or 2 for stereo.

 // create a mono 44.1kHz wave format
 new WaveFormat(44100, 1);