I'm using NAudio 1.7 after i gave up on WaveIn p/invoke... anyway, i'm making a VoIP application and the sample code i found used WaveFileWriter to output to disk, i don't want that, so i used the memoryStream overload instead.
The problem is when i try to play the stream after i stop the recording with the SoundPlayer class, it just doesn't play and continues the code, but if i save it as shown below, i can play it in VLC, but if i try to load it from the file itself, it doesn't play either, this time throwing "Wav header damaged" or something, this throws in the stream if i don't set it to Position 0 too.
Do windows sets the wav file header itself when i save it? Why can VLC play it and SoundPlayer can't even if i save it?
Private waveFile As WaveFileWriter
Private newWave As New NAudio.Wave.WaveIn
Private memstream As New MemoryStream
Public Sub StartRecording()
newWave.WaveFormat = New WaveFormat(44100, 1)
waveFile = New WaveFileWriter(memstream, newWave.WaveFormat)
AddHandler newWave.DataAvailable, AddressOf RecordedDataAvailable
AddHandler newWave.RecordingStopped, AddressOf RecordingStopped
newWave.StartRecording()
End Sub
Private Sub RecordedDataAvailable(sender As Object, e As NAudio.Wave.WaveInEventArgs)
If waveFile IsNot Nothing Then
waveFile.Write(e.Buffer, 0, e.BytesRecorded)
waveFile.Flush()
End If
End Sub
Private Sub RecordingStopped(sender As Object, e As NAudio.Wave.StoppedEventArgs)
Dim fileName As String = Application.StartupPath & "\Test"
File.WriteAllBytes(fileName & ".wav", memstream.GetBuffer)
memstream.Position = 0
Dim player As New SoundPlayer(memstream)
player.Play()
End Sub