Public Class Form1
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim wav1 As List(Of Byte) = IO.File.ReadAllBytes("h.wav").ToList
Dim wav2 As List(Of Byte) = IO.File.ReadAllBytes("e.wav").ToList
wav1 = Join(wav1, wav2)
wav1 = Join(wav1, IO.File.ReadAllBytes("l.wav").ToList)
wav1 = Join(wav1, IO.File.ReadAllBytes("ow.wav").ToList)
Using ms As New System.IO.MemoryStream(wav1.ToArray)
Using snd As New System.Media.SoundPlayer(ms)
snd.Play()
End Using
End Using
End Sub
Private Function Join(ByVal wavBytes As List(Of Byte), ByVal wav2 As List(Of Byte)) As List(Of Byte)
' add on the second file excluding its header bytes
wavBytes.AddRange(wav2.Skip(44).Take(wav2.Count - 44))
' rewrite the ChunkSize and Subchunk2Size fields of the Header
Dim chunkSize As Integer = wavBytes.Count - 8
Dim subChunk2Size As Integer = wavBytes.Count - 44
Dim chunkSizeBytes() As Byte = BitConverter.GetBytes(chunkSize)
Dim subChunk2SizeBytes() As Byte = BitConverter.GetBytes(subChunk2Size)
For i = 0 To 3
wavBytes(4 + i) = chunkSizeBytes(i)
wavBytes(40 + i) = subChunk2SizeBytes(i)
Next
Return wavBytes
End Function
' <summary>
' Function to save byte array to a file
' </summary>
' <param name="_FileName">File name to save byte array</param>
' <param name="_ByteArray">Byte array to save to external file</param>
' <returns>Return true if byte array save successfully, if not return false</returns>
Public Sub ByteArrayToFile(ByVal _FileName As String, ByVal _ByteArray() As Byte)
If System.IO.File.Exists("temp.wav") = True Then
System.IO.File.Delete("temp.wav")
End If
Try
' Open file for reading
Dim _FileStream As New System.IO.FileStream(_FileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
' Writes a block of bytes to this stream using data from a byte array.
_FileStream.Write(_ByteArray, 0, _ByteArray.Length)
' close file stream
_FileStream.Close()
Catch _Exception As Exception
' Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
End Try
End Sub
End Class