C# : concatenate 2 MP3 files

后端 未结 6 2157
小鲜肉
小鲜肉 2021-01-05 13:26

I tried to concatenate 2 MP3 files using the code below. I got a new file which I can play the first half of (complete first file), but the second half is silent. The length

6条回答
  •  情歌与酒
    2021-01-05 14:08

    I am willing to bet you are only hearing the second song. (and that either both files are the same length or the first is shorter)

    You are copying the second song data over the first. And MP3 data is streaming so you can just append the files to each other without worrying about bitrates (while they may glitch) the bitrate should automaticly adjust.

    Detail on MP3 Frame headers

    ... try this...

    Array.Copy(files[0], 0, a, 0, files[0].Length);
    Array.Copy(files[1], 0, a, files[0].Length, files[1].Length);
    

    ... or better still...

    using (var fs = File.OpenWrite(Path.Combine(path, "3.mp3")))
    {
        var buffer = File.ReadAllBytes(Path.Combine(path, "1.mp3"));
        fs.Write(buffer, 0, buffer.Length);
        buffer = File.ReadAllBytes(Path.Combine(path, "2.mp3"));
        fs.Write(buffer, 0, buffer.Length);
        fs.Flush();
    }
    

提交回复
热议问题