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

查看:73
本文介绍了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帧标题的详细信息

...试试这个...

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

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

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