vendredi 2 janvier 2015

Echo sound when record and play from bluetooth headset

I have been finished a project about record and play the sound from Bluetooth headset. It works now. We can speak into headset device and it will record and play directly in speaker of android phone. However, I have a problem that need your help. During the playing, my audio file has echo sound. I am not sure what is happen in my application. But I think the sound may be from MIC in android phone. Please try to look at my project and let me know how to fix it. Thank you in advance This is my sound and my project file download This is my setting



private void initRecorder() {
int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
mBuffer = new short[bufferSize];
mRecorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);


And record and play file



private void startBufferedWrite(final File file) {



new Thread(new Runnable() {
@Override
public void run() {
DataOutputStream output = null;
try {
output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

short[] lin = new short[1024];
int num = 0;
///////////Play during recording
track.play();

while (mIsRecording) {
double sum = 0;
int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
//num = mRecorder.read(lin, 0, 1024);
//track.write(lin, 0, num);
track.write(mBuffer, 0, readSize);
for (int i = 0; i < readSize; i++) {
output.writeShort(mBuffer[i]);
sum += mBuffer[i] * mBuffer[i];

}
if (readSize > 0) {
final double amplitude = sum / readSize;
//AppLog.logString(String.valueOf(((int) Math.sqrt(amplitude))));

double dbAmp=20.0 *Math.log10(amplitude/32767.0);
AppLog.logString(String.valueOf(dbAmp));


}
}
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {

if (output != null) {
try {
output.flush();
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
} finally {
try {
output.close();
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
}
}
}
}).start();
}

_audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxJitter = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);
_audioManager.startBluetoothSco();
_audioManager.setMode(AudioManager.STREAM_SYSTEM);
_audioManager.setSpeakerphoneOn(true);
//_audioManager.setMicrophoneMute(true);
setVolumeControlStream(AudioManager.MODE_IN_COMMUNICATION);
}

Aucun commentaire:

Enregistrer un commentaire