C#:连接 2 个 MP3 文件 [英] C# : concatenate 2 MP3 files

查看:29
本文介绍了C#:连接 2 个 MP3 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码连接 2 个 MP3 文件.我得到了一个新文件,我可以播放(完整的第一个文件)的前半部分,但后半部分是无声的.新文件的长度是正确的.我做错了什么?

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 of the new file was correct. What do I do wrong?

List<Byte[]> files = new List<byte[]>();
var tempfile = File.ReadAllBytes(Path.Combine(path, "1.mp3"));
files.Add(tempfile);
tempfile = File.ReadAllBytes(Path.Combine(path, "2.mp3"));
files.Add(tempfile);
Byte[] a=new Byte[files[0].Length+files[1].Length];
Array.Copy(files[0], a, files[0].Length);
Array.Copy(files[1], a, files[1].Length);

File.WriteAllBytes(Path.Combine(path, "3.mp3") , a);

推荐答案

我敢打赌你只会听到第二首歌.(并且两个文件的长度相同或第一个文件更短)

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)

您正在将第二首歌曲数据复制到第一首歌曲数据上.并且 MP3 数据正在流式传输,因此您可以将文件相互附加而无需担心比特率(虽然它们可能会出现故障)比特率应该自动调整.

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.

MP3 帧头的详细信息

...试试这个...

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();
}

这篇关于C#:连接 2 个 MP3 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆