I have this Sub-Routine which save a WAV-File and also plays it.
It uses the saved WAV-File for the Soundplayer. I would like to use the Stream itself to play (using the lines that are comented), however using it like that I get an error saying "Corrupted WaveHeader". Isn't the "BinWriter.BaseStream" the same Stream that is saved as my *.WAV file?
Can anybody tell me what I'm doing wrong.
vb Code:
  1. Public Sub PlayWaveFile(ByVal WaveFileName As String, ByRef Buffer() As Short, Optional ByVal SamplesPerSec As Integer = 44100)
  2.         Dim BinWriter As BinaryWriter = Nothing
  3.         Dim Filestream As FileStream = Nothing
  4.         Try
  5.             Filestream = New FileStream(WaveFileName, FileMode.Create)
  6.             'Create the writer for data.
  7.             BinWriter = New BinaryWriter(Filestream)
  8.             'Do stuff to create a real Wav-File
  9.             WaveWriteHeader(BinWriter) 'write the Header
  10.             For i As Integer = 0 To UBound(Buffer) - 1 'integrate the created Sound
  11.                 BinWriter.Write(Buffer(i))
  12.             Next i
  13.             WaveWriteHeaderEnd(BinWriter) 'write the Ending
  14.             'Dim Sound As New System.Media.SoundPlayer(BinWriter.BaseStream)
  15.             Dim Sound As New System.Media.SoundPlayer()
  16.             Sound.SoundLocation = ReplayDateiPfad + "\Ping.wav"
  17.             Sound.Play()
  18.             Sound.Dispose()
  19.             Sound = Nothing
  20.             BinWriter.Close()
  21.             Filestream.Close()
  22.         Catch ex As Exception
  23.             ErrLog_schreiben("PlayWaveFile", ex.Message, WaveFileName)
  24.         End Try
  25.     End Sub