我使用AudioRecord
录制来自Android的音频,然后使用AudioTrack
回复它。然而,结果是非常可怕的,它在某种程度上类似于它记录的,但它可能更慢(或者可能因为它被修改,所以我觉得这样)。当我仔细分析时,我意识到我的short
数组中存在许多“间隙”(0值)。
这是我收到的图表:
差距(它重复这种模式,每个约630字节的数据,大约有630字节的0):
以下是我的录音代码:
protected void onRecordButtonClick() {
if (this.recording) {
this.recording = false;
this.butRecord.setText("Record");
} else {
this.recordBufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
this.recorder = new AudioRecord(
MediaRecorder.AudioSource.DEFAULT,
44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
this.recordBufferSize);
this.recordThread = new Thread(new Runnable() {
@Override
public void run() {
MainActivity.this.onRecording();
}
});
this.recordThread.setPriority(Thread.MAX_PRIORITY);
this.recording = true;
this.butRecord.setText("Stop recording");
this.recordThread.start();
}
}
protected void onRecording() {
this.recordData.clear();
final short[] temp = new short[this.recordBufferSize];
this.recorder.startRecording();
while (this.recording) {
this.recorder.read(temp, 0, this.recordBufferSize);
for (int i = 0; i < temp.length; i ++) {
this.recordData.add(temp[i]);
}
if (this.recordData.size() >= 220500) { this.recording = false; }
}
this.recorder.stop();
// Complete data
this.currentData = new short[this.recordData.size()];
for (int i = 0; i < this.currentData.length; i++) {
this.currentData[i] = this.recordData.get(i);
}
{ // Write to SD Card the result
final File file = new File(Environment.getExternalStorageDirectory(), "record.pcm");
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < this.currentData.length; i++) {
oos.writeShort(this.currentData[i]);
}
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
this.handler.post(new Runnable() {
@Override
public void run() {
MainActivity.this.waveform.setData(MainActivity.this.currentData);
MainActivity.this.butRecord.setText("Record");
final Bitmap bitmap = MainActivity.this.waveform.getDrawingCache();
try {
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "cache.png"));
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}