我正在尝试在Android中添加两个wav文件。因为我将两个文件读取到2个字节数组(A& B)。我将它们两个组合到一个新数组(C)。我写了内容第三个数组C到一个新的.wav输出文件。我也把wav头文件。我得到大小的输出文件(C = A + B)。但是播放器无法播放该输出文件。
如果第二个输入文件(B)没有读取到字节数组(这里我初始化B。但是没有将第二个文件的内容写入字节数组,B),那么我可以播放输出文件,这里是第一个文件完全播放但播放器静音后(代替第二档)..
我使用了以下代码片段:请帮助解决此问题
File firstFile=new File("path1.wav");
File secondFile=new File("path2.wav");
int size1=(int)firstFile.length();
int size2=(int)secondFile.length();
Log.i("Size1::"+size1,"Size2::"+size2);
dataOne=new byte[size1];
dataTwo=new byte[size2];
newData=new byte[dataOne.length+dataTwo.length];
try
{
in1=new DataInputStream(new BufferedInputStream(new FileInputStream(firstFile)));
in2=new DataInputStream(new BufferedInputStream(new FileInputStream(secondFile)));
in1.readFully(dataOne);
in1.close();
in2.read(dataTwo);
in2.close();
int j;
for(j=0;j<dataOne.length;j++)
newData[j]=dataOne[j];
for(int m=0;m<dataTwo.length;m++)
newData[j+m]=dataTwo[j];
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
fWriter = new FileOutputStream("outputPath.wav");
WAVE(fWriter);
//fWriter.write(newData,0,newData.length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我使用以下两种方法创建wav标头
public void WAVE(FileOutputStream fWriter)
{
try
{
//0------------3
fWriter.write('R');
fWriter.write('I');
fWriter.write('F');
fWriter.write('F');
//4------7
byte[] res=new byte[4];
int sizeOfFile=newData.length-8;
res=toBytes(sizeOfFile);
for(int j=0;j<4;j++)
Log.i(""+j,""+res[j]);
fWriter.write(res,0,4);
//8-15
fWriter.write('W');
fWriter.write('A');
fWriter.write('V');
fWriter.write('E');
fWriter.write('f');
fWriter.write('m');
fWriter.write('t');
fWriter.write(' ');
//16----19
res=new byte[4];
res=toBytes(16);
for(int j=0;j<4;j++)
Log.i(""+j,""+res[j]);
fWriter.write(res,0,4);
//20---21
res=toBytes(1);
for(int j=0;j<2;j++)
Log.i("PCM"+j,""+res[j]);
fWriter.write(res,0,2);
//22---23
res=toBytes(2);
for(int j=0;j<2;j++)
Log.i("NOC:MONO/STEREO"+j,""+res[j]);
fWriter.write(res,0,2);
//24--27
int ASR=Integer.parseInt(44100);
res=toBytes(ASR);
for(int j=0;j<4;j++)
Log.i(ASR+"ASR"+j,""+res[j]);
fWriter.write(res,0,4);
//28--31 Bytes Per Second
int Number_of_channels=2;
int Samples_per_second=ASR;
int Bits_per_Sample=32;
int BPS=Number_of_channels*Samples_per_second*Bits_per_Sample/8;
Integer.parseInt(audioSamplingRate);
res=toBytes(BPS);
for(int j=0;j<4;j++)
Log.i(ASR+"BPS"+j,""+res[j]);
fWriter.write(res,0,4);
//32--33 Block Alignment
int BA= Number_of_channels*Bits_per_Sample/8;
res=toBytes(BA);
for(int j=0;j<2;j++)
Log.i("Block Alignment"+j,""+res[j]);
fWriter.write(res,0,2);
//34---35 Bits Per Sample
res=toBytes(Bits_per_Sample);
for(int j=0;j<2;j++)
Log.i(ASR+"Bits Per Sample"+j,""+res[j]);
fWriter.write(res,0,2);
//36---39 "data"
fWriter.write('d');
fWriter.write('a');
fWriter.write('t');
fWriter.write('a');
//40----43 length of data
res=toBytes(newData.length);
for(int j=0;j<4;j++)
Log.i("Data Size"+j,""+res[j]);
fWriter.write(res,0,4);
fWriter.write(newData);
fWriter.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public byte[] toBytes(int i)
{
byte[] result = new byte[4];
result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i /*>> 0*/);
return result;
}
我使用API 8(2.2 Froyo)