3

I just wanted to make a app that works as an loudspeaker. Whatever gets into microphone(headphones) must be played through external speaker. I tried some and i followed the following link procedures but i couldn't make it. Is there any other way i can get this out. Please help me. Thanks in advance.

Android - Getting audio to play through earpiece

how to play sound from microphone to speaker directly on android?

boolean isRecording; //currently not used
AudioManager am;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    Record record = new Record();
    record.run();

}

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);

        AudioRecord arec = new AudioRecord
        (MediaRecorder.AudioSource.MIC,
                11025,

                AudioFormat.CHANNEL_CONFIGURATION_MONO,

                AudioFormat.ENCODING_PCM_16BIT,
                buffersize);

        AudioTrack atrack = new AudioTrack
        (AudioManager.STREAM_VOICE_CALL,
                11025,

                AudioFormat.CHANNEL_CONFIGURATION_MONO,

                AudioFormat.ENCODING_PCM_16BIT,
                buffersize,

                AudioTrack.MODE_STREAM);

        am.setRouting(AudioManager.MODE_NORMAL,
                AudioManager.ROUTE_EARPIECE,

                AudioManager.ROUTE_ALL);

        Log.d("SPEAKERPHONE", "Is speakerphone on? : " + am.isSpeakerphoneOn());

        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);
        }

        arec.stop();
        atrack.stop();
        isRecording = false;
    }
}
Community
  • 1
  • 1
GoSmash
  • 1,096
  • 1
  • 11
  • 38
  • 3
    If you succeed at this, it's going to cause a nice feedback loop. – Thomas Dignan Apr 07 '12 at 13:43
  • Tom that why now i planned to play audio in earpiece, I know if it is played through loudspeaker it will cause lot of trouble. My plan is to make an app that helps to cancel the acoustic noise. So if i able to get the audio straight to earpiece i can encode the audio wave and use it for noise reduction. Am i right ?? – GoSmash Apr 09 '12 at 06:14
  • @Ramesh: I don't think that this can be used for noise reduction. There are a bunch of problems that would be hard to overcome: variable positioning would defeat any calibration; time delay and physical separation would make this only work for low frequencies, if even that; etc. – tom10 Apr 11 '12 at 04:07

0 Answers0