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();
}
}
});
}