I recorded an audio file A and saved it as testaudio0.gp Then I recorded an audio file B while audio file A was playing at the same time and saved it as testaudio.gp
Of course, in audio file B I hear audio file A as well.
I use the normal MediaPlayer and MediaRecorder classes in Android. The audio files are of the same length. The file size is 6,81Kb for both.
Here is my Code:
final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/testaudio.3gp");
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
Uri myUri = Uri.parse("/sdcard/testaudio0.3gp"); // initialize Uri here
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(), myUri);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
recorder.start(); // Recording is now started
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Recording Stopped!", Toast.LENGTH_SHORT).show();
}
});
}
}, 4000);
As you can see I changed recorder.setAudioSource
to VOICE_COMMUNICATION since after doing some research on echo cancellation I read that this should help. It seems like it helps a bit, but there is still a very good hearable background audio of file A existing.
I would like to subtract as much as possible of audio file A . I did not find a stack exchange question answering how to do this. Please be patient that I do not understand those complicated mathematical operations to do. A code example would help a lot.
Any help is appreciated. Thank you.