Note that there are some explanatory texts on larger screens.

plurals
  1. POnaudio record sound from microphone then save
    primarykey
    data
    text
    <p>I'm having some issues with naudio and saving sound recordings. The code I currently have works to the point where it saves the wav file, but when I open it up, Windows Media Player returns an error: "Windows Media Player encountered a problem while playing the file"</p> <p>I have two buttons, a "Record" button, which turns into the stop button after it's pressed. And I have a "Save" button which when clicked, saves the recording to <code>sample.wav</code>.</p> <pre><code>NAudio.Wave.WaveIn sourceStream = null; NAudio.Wave.DirectSoundOut waveOut = null; NAudio.Wave.WaveFileWriter waveWriter = null; private void recordButton_Click(object sender, EventArgs e) { int deviceNumber = sourceList.SelectedItems[0].Index; sourceStream = new NAudio.Wave.WaveIn(); sourceStream.DeviceNumber = deviceNumber; sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels); NAudio.Wave.WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(sourceStream); waveOut = new NAudio.Wave.DirectSoundOut(); waveOut.Init(waveIn); sourceStream.StartRecording(); waveOut.Play(); recordButton.Visible = false; stopRecord.Visible = true; } private void saveResponse_Click(object sender, EventArgs e) { int deviceNumber = sourceList.SelectedItems[0].Index; string saveLocation = "c:\\wav\\sample.wav"; sourceStream = new NAudio.Wave.WaveIn(); sourceStream.DeviceNumber = deviceNumber; sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels); sourceStream.DataAvailable += new EventHandler&lt;NAudio.Wave.WaveInEventArgs&gt;(sourceStream_DataAvailable); waveWriter = new NAudio.Wave.WaveFileWriter(saveLocation, sourceStream.WaveFormat); sourceStream.StartRecording(); MessageBox.Show("Recording successfully saved."); } private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e) { if (waveWriter == null) return; waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded); waveWriter.Flush(); } private void stopRecord_Click(object sender, EventArgs e) { if (waveOut != null) { waveOut.Stop(); waveOut.Dispose(); waveOut = null; } if (sourceStream != null) { sourceStream.StopRecording(); sourceStream.Dispose(); sourceStream = null; } if (waveWriter != null) { waveWriter.Dispose(); waveWriter = null; } recordButton.Visible = true; stopRecord.Visible = false; saveResponse.Enabled = true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload