title: Android - Getting audio to play through earpiece
body: <p>I currently have code that reads a recording in from the devices mic using the AudioRecord class and then playing it back out using the AudioTrack class.</p>
  
  <p>My problem is that when I play it out it plays via the speaker phone.</p>
  
  <p>I want it to play out via the ear piece on the device.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class LoopProg extends Activity {
  
   boolean isRecording; //currently not used
   AudioManager am;
   int count = 0;
  
   /** 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);
          am.setMicrophoneMute(true);
          while(count &lt;= 1000000){
          Record record = new Record();  
          record.run();
          count ++;
          Log.d("COUNT", "Count is : " + count);
          }
      } 
  
     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_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);
        am.setRouting(AudioManager.MODE_NORMAL,1, AudioManager.STREAM_MUSIC);
        int ok = am.getRouting(AudioManager.ROUTE_EARPIECE);
        Log.d("ROUTING", "getRouting = " + ok);
        setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
        //am.setSpeakerphoneOn(true);
        Log.d("SPEAKERPHONE", "Is speakerphone on? : " + am.isSpeakerphoneOn());
        am.setSpeakerphoneOn(false);
        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;
        }
     } 
  }
  </code></pre>
  
  <p>As you can see if the code I have tried using the AudioManager class and its methods including the deprecated setRouting method and nothing works, the setSpeakerphoneOn method seems to have no effect at all, neither does the routing method.</p>
  
  <p>Has anyone got any ideas on how to get it to play via the earpiece instead of the spaker phone?</p>