我正在尝试连接字节数组音频。(音频是.wav,并转换为字节数组)。 我目前的代码似乎连接起来。但是当我播放结果时,它只播放最终数组中的第一个字节样本。 你知道为什么下一个字节不播放吗?
int length = 0;
for (byte[] array : ListSamplesByte) {
length += array.length;
}
byte[] result = new byte[length];
int pos = 0;
for (byte[] array : ListSamplesByte) {
System.arraycopy(array, 0, result, pos, array.length);
pos += array.length;
}
byte[] playByte = result;
MediaPlayer.playAudio(playByte);
编辑1:所有文件都是44100Hz,16位PCM。即使所有字节数组都相同,它也只播放第一个字节数组。我认为问题与块中没有添加或删除的标题或字节有关。
答案 0 :(得分:1)
我找到了解决方案。 这是连接的过程,请注意我所有的wav文件具有相同的采样率,通道。
首先,我需要一些关于WAV文件结构的知识。 wav文件由原始数据后跟的头(前44个字节)组成。 http://soundfile.sapp.org/doc/WaveFormat/
1)将您的wav文件转换为字节数组
2)查看HeaderInformation()下面的函数,它接受你的字节数组的参数,它创建一个InputDataStream并读取bytes数组以获取头信息,最重要的是得到ChunkSize长度(字节4 -8)和原始数据长度(字节44)。
public class AudioConcatenate {
long myChunkSize;
long mySubChunk1Size;
int myFormat;
long myChannels;
long mySampleRate;
long myByteRate;
int myBlockAlign;
int myBitsPerSample;
long myDataSize;
long totalChunkSize;
byte[] myData;
int byteRate=176400;
long longSampleRate=44100;
protected ArrayList<Long> ListDataSize= new ArrayList<Long>();
protected ArrayList<byte[]> ListDataByte = new ArrayList<byte[]>();
protected ArrayList<Long> ListChunkSize = new ArrayList<>();
public AudioConcatenate(){
}
protected void HeaderInformation(byte[] SoundSample) {
myData = null;
DataInputStream inFile = newDataInputStream(newByteArrayInputStream(sample));
byte[] tmpLong = new byte[4];
byte[] tmpInt = new byte[2];
try {
while (inFile.available() > 0) {
String chunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();
inFile.read(tmpLong); // read the ChunkSize
myChunkSize = byteArrayToLong(tmpLong)+silenceArray.length;
String format = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();
String subChunk1ID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();
inFile.read(tmpLong); // read the SubChunk1Size
mySubChunk1Size = byteArrayToLong(tmpLong);
inFile.read(tmpInt); // read the audio format. This should be 1 for PCM
myFormat = byteArrayToInt(tmpInt);
inFile.read(tmpInt); // read the # of channels (1 or 2)
myChannels = byteArrayToInt(tmpInt);
inFile.read(tmpLong); // read the samplerate
mySampleRate = byteArrayToLong(tmpLong);
inFile.read(tmpLong); // read the byterate
myByteRate = byteArrayToLong(tmpLong);
inFile.read(tmpInt); // read the blockalign
myBlockAlign = byteArrayToInt(tmpInt);
inFile.read(tmpInt); // read the bitspersample
myBitsPerSample = byteArrayToInt(tmpInt);
String dataChunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();
inFile.read(tmpLong); // read the size of the data
myDataSize = byteArrayToLong(tmpLong)+silenceArray.length;
// read the data chunk
myData = new byte[(int) myDataSize];
inFile.read(myData);
ListDataSize.add(myDataSize);
ListDataByte.add(FinalDataArray);
ListChunkSize.add(myChunkSize);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
函数末尾的byte []三个arraylist用于存储块大小,存储原始数据的字节数组和数据字节数组的大小。这样,每次调用声音函数时,它都会存储基本数据。
4)一旦你为多声音调用此功能。 我们需要重新创建wav标头,它将计算多个声音的长度。我们必须计算ChunkSize的总大小,以及原始数据的总大小。
public long TotalDataSize(){
int i;
long sum = 0;
for(i = 0; i < ListDataSize.size(); i++) {
sum += ListDataSize.get(i);
}
return sum;
}
public long TotalChunkSize(){
int i;
long sum = 0;
for(i = 0; i < ListChunkSize.size(); i++) {
sum += ListChunkSize.get(i);
}
return sum;
}
We create the header and we include the new size, total chunkSize at byte [4-8], total data byte[40-43]
public byte[] WriteHeader(){
byte[] header = new byte[44];
long totalDataSize= TotalDataSize();
long totalChunkSize=TotalChunkSize();
header[0] = 'R'; // RIFF/WAVE header
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (byte) (totalChunkSize & 0xff);
header[5] = (byte) ((totalChunkSize >> 8) & 0xff);
header[6] = (byte) ((totalChunkSize >> 16) & 0xff);
header[7] = (byte) ((totalChunkSize >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f'; // 'fmt ' chunk
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 16; // 4 bytes: size of 'fmt ' chunk
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1; // format = 1
header[21] = 0;
header[22] = (byte) 2;
header[23] = 0;
header[24] = (byte) (longSampleRate & 0xff);
header[25] = (byte) ((longSampleRate >> 8) & 0xff);
header[26] = (byte) ((longSampleRate >> 16) & 0xff);
header[27] = (byte) ((longSampleRate >> 24) & 0xff);
header[28] = (byte) (byteRate & 0xff);
header[29] = (byte) ((byteRate >> 8) & 0xff);
header[30] = (byte) ((byteRate >> 16) & 0xff);
header[31] = (byte) ((byteRate >> 24) & 0xff);
header[32] = (byte) (2 * 16 / 8); // block align
header[33] = 0;
header[34] = 16; // bits per sample
header[35] = 0;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte) (totalDataSize & 0xff);
header[41] = (byte) ((totalDataSize >> 8) & 0xff);
header[42] = (byte) ((totalDataSize >> 16) & 0xff);
header[43] = (byte) ((totalDataSize >> 24) & 0xff);
return header;
}
6)连接函数: 首先,我们创建一个新的ArrayList“ListFinalSound”来构造最终的声音。 我们首先添加新标题,然后添加byte []原始数据。
public byte[] ConcatenateSound(){
ListFinalSound.add(WriteHeader());
for (byte[] arrayRawData : ListDataByte) {
ListFinalSound.add(arrayRawData);
}
/* sum the length of each byte array to get the total and create later a new byte array of the total length.**/
int length = 0;
for (byte[] array : ListFinalSound) {
length += array.length;
}
/* we concatenate with the use of System.arraycopy.
For each loop it'll increment the position and concatenate the byte array**/
byte[] ConcatenateSounds = new byte[length];
int pos = 0;
for (byte[] array : ListFinalSound) {
System.arraycopy(array, 0, ConcatenateSounds, pos, array.length);
pos += array.length;
}
return ConcatenateSounds;
}
7)ConcatenateSound()函数返回连接。