Reputation: 927
I am using the following code to Implement a live Recording & Playback system :
import android.media.*;
class Rec {
static boolean m_isRun = true;
static int buffersize, SAMPLE_RATE = 22050;
public static void loopback()
{
AudioRecord m_record = null;
AudioTrack m_track = null;
// Prepare the AudioRecord & AudioTrack
try {
buffersize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
//Log.i(LOG_TAG,"Initializing Audio Record and Audio Playing objects");
m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffersize * 1);
m_track = new AudioTrack(AudioManager.STREAM_MUSIC,
SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffersize * 1,
AudioTrack.MODE_STREAM);
m_track.setPlaybackRate(SAMPLE_RATE);
} catch (Throwable t) {
// Log.e("Error", "Initializing Audio Record and Play objects Failed "+t.getLocalizedMessage());
}
m_record.startRecording();
//Log.i(LOG_TAG,"Audio Recording started");
m_track.play();
//Log.i(LOG_TAG,"Audio Playing started");
byte buffer[] = new byte[buffersize];
while (m_isRun)
{
m_record.read(buffer, 0, buffer.length);
m_track.write(buffer, 0, buffer.length);
}
}
public static void do_loopback() {
new Thread(new Runnable() {
public void run() {
loopback();
}
}).start();
}
}
I have earphones plugged in.
As you can see, Input audio is coming from Mic (i.e from the recording chip within the Smartphone) while the output audio is routed ONLY to earphones (due to STREAM_MUSIC stream type of AudioTrack).
But my problem is that, it results in Echo during playback. I think, the Echo is not due to recording of the Playback audio because the Play back audio is played only through earphones plugged in.And also the Earphones are placed nearly 1 to 2 feet apart from the recording Phone.
So what is causing the Echo?
Upvotes: 2
Views: 1430
Reputation: 125
Try setting the minBuffer and the MediaRecorder to "AudioFormat.CHANNEL_IN_STEREO" I also set the AudioTrack to "AudioTrack.PERFORMANCE_MODE_LOW_LATENCY" and "AudioFormat.CHANNEL_OUT_STEREO", In my case this work for me with clear sound output.
boolean isRecording;
private int minBuffer;
private int sampleRate = 44100;
void loop() {
AudioRecord mAudioRecord = null;
AudioTrack mAudioTrack = null;
try {
minBuffer = AudioRecord.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
Log.d(TAG, "loop: " + minBuffer);
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleRate,
AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT, minBuffer);
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
minBuffer,
AudioTrack.PERFORMANCE_MODE_LOW_LATENCY);
mAudioTrack.setPlaybackRate(sampleRate);
} catch (Exception e) {
Log.d(TAG, "startStream: " + e.getMessage());
} finally {
try {
mAudioRecord.startRecording();
} catch (IllegalStateException e) {
Log.d(TAG, "loop: " + e.getMessage());
}
}
mAudioTrack.play();
byte[] buffer = new byte[minBuffer];
while (isRecording) {
mAudioRecord.read(buffer, 0, buffer.length);
mAudioTrack.write(buffer, 0, buffer.length);
}
}
Upvotes: 3