How to merge to music file into a one separate file in VB.NET ?

sa pir
sa pir used Ask the Experts™
on
the following code that i have tried merges to music file but only first music plays because of length or something. !!!
Dim s1 As Byte() = File.ReadAllBytes("e:\Music\Voice 001.mp3")
        Dim s2 As Byte() = File.ReadAllBytes("e:\Music\Voice 002.mp3")
        Dim adWithSong As Byte() = New Byte(s1.Length + s2.Length) {}
        s1.CopyTo(adWithSong, 0)
        s2.CopyTo(adWithSong, s1.Length)
        Dim newFilePath As String = "e:\Music\test1.mp3"
        File.WriteAllBytes(newFilePath, adWithSong)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
DarrenSenior Software Engineer

Commented:
You will have to cut off the first few bytes on the second MP3 before merging.

I've done this before but I'll have to try to did out the code.
Senior Software Engineer
Commented:
I ended up creating my sounds as mp3's with IDv1 data. So in order to play my sounds correctly and in the order I specified I cut off the last 128 bits as these are the ID tags and concatenated the binary of multiple file together and created a temoorary file and played this.

    Private Function PlayVoice(ByVal TicketLetter As String, ByVal TicketNumber As String, ByVal Counter As String) As Boolean

      Dim Result As Integer
      Dim Seek As Long

      Dim Play() As Byte

      Dim Doorbell() As Byte
      Dim Ticket() As Byte
      Dim Number1() As Byte
      Dim Number2() As Byte
      Dim Number3() As Byte
      Dim CounterNumber() As Byte

      Dim Blank() As Byte

      ' Open the Ticket mp3 file.
      Dim MyBinaryReader = New BinaryReader(File.OpenRead("C:\Downloads\Sounds\Melanie Sounds\Splice\T" & TicketLetter & ".mp3"))
      ReDim Ticket(MyBinaryReader.BaseStream.Length - (1 + 128))
      Result = MyBinaryReader.Read(Ticket, 0, MyBinaryReader.BaseStream.Length - 128)

      ' Open the 1st Number mp3 file.
      MyBinaryReader = New BinaryReader(File.OpenRead("C:\Downloads\Sounds\Melanie Sounds\Splice\Number\" & TicketNumber.Substring(0, 1) & ".mp3"))
      ReDim Number1(MyBinaryReader.BaseStream.Length - (1 + 128))
      Result = MyBinaryReader.Read(Number1, 0, MyBinaryReader.BaseStream.Length - 128)

      ' Open the 2nd Number mp3 file.
      MyBinaryReader = New BinaryReader(File.OpenRead("C:\Downloads\Sounds\Melanie Sounds\Splice\Number\" & TicketNumber.Substring(1, 1) & ".mp3"))
      ReDim Number2(MyBinaryReader.BaseStream.Length - (1 + 128))
      Result = MyBinaryReader.Read(Number2, 0, MyBinaryReader.BaseStream.Length - 128)

      ' Open the 3rd Number mp3 file.
      MyBinaryReader = New BinaryReader(File.OpenRead("C:\Downloads\Sounds\Melanie Sounds\Splice\Number\" & TicketNumber.Substring(2, 1) & ".mp3"))
      ReDim Number3(MyBinaryReader.BaseStream.Length - (1 + 128))
      Result = MyBinaryReader.Read(Number3, 0, MyBinaryReader.BaseStream.Length - 128)

      ' Open the Counter Number mp3 file.
      MyBinaryReader = New BinaryReader(File.OpenRead("C:\Downloads\Sounds\Melanie Sounds\Splice\Counter\C" & Counter & ".mp3"))
      ReDim CounterNumber(MyBinaryReader.BaseStream.Length - 1)
      Result = MyBinaryReader.Read(CounterNumber, 0, MyBinaryReader.BaseStream.Length - 1)

      ReDim Play(Ticket.Length + Number1.Length + Number2.Length + Number3.Length + CounterNumber.Length)

      Ticket.CopyTo(Play, 0)
      Number1.CopyTo(Play, Ticket.Length)
      Number2.CopyTo(Play, Ticket.Length + Number1.Length)
      Number3.CopyTo(Play, Ticket.Length + Number1.Length + Number2.Length)
      CounterNumber.CopyTo(Play, Ticket.Length + Number1.Length + Number2.Length + Number3.Length)

      Dim TmpFileName As String = Path.GetTempFileName()
      Dim TmpFileNameNoExtension As String = Path.GetFileNameWithoutExtension(TmpFileName)

      Dim MyBinaryWriter As BinaryWriter = New BinaryWriter(File.OpenWrite("C:\Downloads\" & TmpFileNameNoExtension & ".mp3"))

      MyBinaryWriter.Write(Play)

      MyBinaryReader.Close()
      MyBinaryWriter.Close()

      MyPlayer.playlistClear()
      MyPlayer.addTarget("C:\Downloads\" & TmpFileNameNoExtension & ".mp3", Nothing, AXVLC.VLCPlaylistMode.VLCPlayListReplaceAndGo, 0)

      'File.Delete("C:\Downloads\" & TmpFileNameNoExtension & ".mp3")

    End Function
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
MP3s have header information as well as the music data. You can't just splice together the bytes of two MP3s because you'll be putting header-music-header-music into the file. In effect, the 2nd song won't exist. You need to parse out the music data from the second file and append it to the first song's music data. You'll also need to update the spliced file's header information to account for the new length.

MP3 is actually a proprietary format. You can find some information at the following link:

http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
DarrenSenior Software Engineer

Commented:
Closing as question was answered

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial