C# : concatenate 2 MP3 files

后端 未结 6 2166
小鲜肉
小鲜肉 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 13:50

    Here's how you can concatenate MP3 files using NAudio:

    public static void Combine(string[] inputFiles, Stream output)
    {
        foreach (string file in inputFiles)
        {
            Mp3FileReader reader = new Mp3FileReader(file);
            if ((output.Position == 0) && (reader.Id3v2Tag != null))
            {
                output.Write(reader.Id3v2Tag.RawData,
                             0,
                             reader.Id3v2Tag.RawData.Length);
            }
            Mp3Frame frame;
            while ((frame = reader.ReadNextFrame()) != null)
            {
                output.Write(frame.RawData, 0, frame.RawData.Length);
            }
        }
    }
    

    See here for more information.

提交回复
热议问题