Android 字节数组音频连接
我正在尝试连接字节数组音频。(音频为.wav,并转换为字节数组)。 我当前的代码似乎是串联的。但是当我播放结果时,它只播放最终数组中的第一个字节样本。 你知道为什么下一个字节不能播放吗Android 字节数组音频连接,android,arrays,audio,byte,concatenation,Android,Arrays,Audio,Byte,Concatenation,我正在尝试连接字节数组音频。(音频为.wav,并转换为字节数组)。 我当前的代码似乎是串联的。但是当我播放结果时,它只播放最终数组中的第一个字节样本。 你知道为什么下一个字节不能播放吗 int length = 0; for (byte[] array : ListSamplesByte) { length += array.length; } byte[] result = new byte[length]; int pos = 0;
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。即使所有字节数组都相同,它也只播放第一个字节数组。我认为问题与块中未添加或删除的头或字节有关。我找到了解决方案。 这里是连接的过程,注意我所有的wav文件都有相同的采样率,通道 首先,我需要一些关于WAV文件结构的知识。wav文件由头文件(前44个字节)和原始数据组成 1) 将wav文件转换为字节数组 2) 看看HeaderInformation()下面的函数,它接受参数your byte array,它创建一个InputDataStream并读取字节数组,目的是获取头信息,最重要的是获取ChunkSize长度(字节4-8)和原始数据长度(字节44)
7) 函数的作用是:返回连接。你所有的
WAV
文件都是相同的吗?所有的文件都是44100Hz,16位PCM。即使所有字节数组都相同,它也只播放第一个字节数组。我认为问题与块中未添加或删除的头或字节跳过有关RocéTarentula Yesterday您是否有一个最小的工作项目示例,来尝试和测试一些东西?你的问题值得更多的关注,但因为我是唯一一个在这一刻投了赞成票的人,我会尽我所能帮助你!谢谢Diti,我找到了连接的解决方案,但实际上我需要的是混合数组。我只是锁定了数组的和。
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();
}
}
}
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;
}
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;
}