button.setOnClickListener(v -> {
simpleRateInHz = 22050;
bufferSize = AudioRecord.getMinBufferSize(simpleRateInHz, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, simpleRateInHz, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
whetherRecord = true;
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, simpleRateInHz, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
//开始录音
a();
});
}
private void a() {
new Thread(() -> {
audioRecord.startRecording();
while (true) {
short[] audioBuffer = new short[bufferSize];
int bufferReadResult = audioRecord.read(audioBuffer, 0, bufferSize);
audioTrack.write(audioBuffer, 0, bufferReadResult);
audioTrack.play();
}
}).start();
}
最基础的实现方式, 资源释放, 及参数设置等, 参考其他大神的文章.
网友评论