【发布时间】:2015-10-04 19:45:19
【问题描述】:
我在将两个不同的音频样本混合为一个时遇到问题,只需将两个音频样本的字节相加即可。
当我尝试在媒体播放器中打开混合.mp3 文件时,经过以下过程,它显示:
Windows Media Player 在播放文件时遇到问题。
这是我用来混合音频文件的代码:
byte[] bytes1,bytes2,final;
int length1,length2,max;
// Getting byte[] of audio file
using ( BinaryReader b = new BinaryReader(File.Open("background.mp3" , FileMode.Open)) )
{
length1 = (int)b.BaseStream.Length;
bytes1 = b.ReadBytes(length1);
}
using ( BinaryReader b = new BinaryReader(File.Open("voice.mp3" , FileMode.Open)) )
{
length2 = (int)b.BaseStream.Length;
bytes2 = b.ReadBytes(length2);
}
// Getting max length
if(length1 > length2){
max = length1;
}else{
max = length2;
}
// Initializing output byte[] of max length
final = new byte[max];
// Adding byte1 and byte2 and copying into final
for (int i=0;i<max;i++)
{
byte b1 , b2;
if(i < length1){
b1 = bytes1[i];
}else{
b1 = 0;
}
if ( i < length2 ){
b2 = bytes2[i];
}
else{
b2 = 0;
}
final[i] = (byte)(b1 + b2);
}
// Writing final[] as an mp3 file
File.WriteAllBytes("mixed.mp3" , final);
注意:我尝试混合两个相同的文件,并且成功了,也就是说,媒体播放器没有抛出任何错误并且可以正确播放。
【问题讨论】:
-
你的底注是什么意思?你的意思是代码现在可以工作了?
-
这是不可能的。 Mp3 不是行波数据。像这样添加字节会 100% 损坏数据。不确定它是如何为您工作的
-
解码,计算,记住裁剪(0..255),再次编码。您现在所做的无法正常工作,因为:mp3 已压缩(除非它仅包含幅度数据……我不是专家),您还必须考虑在文件级别工作时不应更改的所有元数据的标题。
-
@TaLha Khan 我的回答对你有用吗?
-
@EmpereurAiman 没有。你的答案和我正在做的一样,我的意思是附加字节。