我一直在尝试使用AudioRecord实时从设备麦克风获取音频。我将从AudioRecord获取的每个byte []放入一个ArrayList并记下经过的时间,然后当ArrayList达到一定大小时,我打印间隔记录。然而,当我打印这些间隔时,我了解到从AudioRecord读取每个新字节[]之间大约有20毫秒,而字节[] s本身只有81个条目长,而不是1024.是否有可能解决这个问题,所以我可以即时读取音频数据吗?
private void writeAudioDataToFile() {
// Write the output audio in byte
short sData[] = new short[BufferElements2Rec];
long startTime = time();
long elapsed = 0;
final long BUFFER_SIZE = 2000; //in millis
//initialize buffer creation time variable to time just before creation of first buffer
long bufferCreationTime = time();
List<Long> time = new ArrayList<Long>();
ArrayList<byte[]> byteBuffer = new ArrayList<byte[]>();
while (isRecording) {
// gets the voice output from microphone to byte format
//TODO: absolute value averages of block data
final int BYTE_ARRAY_SIZE = 1024;
final int TIME_BYTE_ARRAY_SIZE = 8;
//read the next short array of audio data
audioRecord.read(sData, 0, BufferElements2Rec);
System.out.println("Short writing to file" + " " + elapsed + " " + Arrays.toString(sData));
System.out.println("" + elapsed + " " + " " + BUFFER_SIZE);
try {
// // stores the voice buffer
final byte bData[] = short2byte(sData);
byteBuffer.add(bData);
time.add(time());
elapsed = time() - startTime;
if(elapsed > BUFFER_SIZE) {
System.out.println(time);
for (int i = 0; i < time.size(); i++)
time.set(i, time.get(i) - bufferCreationTime);
System.out.println(time);
System.out.println("TAG" + blockedBuffer);
}
//set new buffer creation time
bufferCreationTime = time();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
*/
}