我目前拥有一个用于测试Android设备音频的回环程序。
它使用AudioRecord和AudioTrack从麦克风录制PCM音频,并将PCM音频通过听筒播放出来。
以下是代码:
public class Record extends Thread
{
static final int bufferSize = 200000;
final short[] buffer = new short[bufferSize];
short[] readBuffer = new short[bufferSize];
public void run() {
isRecording = true;
android.os.Process.setThreadPriority
(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
int buffersize = AudioRecord.getMinBufferSize(11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
buffersize);
atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
buffersize,
AudioTrack.MODE_STREAM);
atrack.setPlaybackRate(11025);
byte[] buffer = new byte[buffersize];
arec.startRecording();
atrack.play();
while(isRecording) {
arec.read(buffer, 0, buffersize);
atrack.write(buffer, 0, buffer.length);
}
}
}
正如您在创建AudioTrack和AudioRecord时所看到的,编码是通过AudioFormat提供的,但这仅允许16位或8位PCM。
我现在有自己的G711编解码器实现,并希望能够对来自麦克风的音频进行编码并对进入EarPiece的音频进行解码,因此我有encode(short lin[], int offset, byte enc[], int frames)和decode(byte enc[], short lin[], int frames)方法,但我不确定如何使用它们来对AudioRecord和AudioTrack中的音频进行编码和解码。
有人可以帮助我或指点我方向吗?